﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading;
using System.IO.Ports;
using System.Windows.Forms;
using Infrastructure;
using Picking.Common;
using PickingBusiness;
using LabelManager2;
using Infrastructure.Util;

namespace Picking_Winform
{
    public partial class DistributionOperationView : FormBase
    {
        private int currentWorkerCountLine1 = 0;
        private int currentWorkerCountLine2 = 0;
        private int currentWorkerCountLine3 = 0;
        private bool lightEnbaleLine1 = true;
        private bool lightEnbaleLine2 = true;
        private bool lightEnbaleLine3 = true;

        private bool scannerEnbaleLine1 = false;
        private bool scannerEnbaleLine2 = false;
        private bool scannerEnbaleLine3 = false;

        private string scannerPortWorker1Line1 = string.Empty;
        private string scannerPortWorker2Line1 = string.Empty;
        private string scannerPortWorker3Line1 = string.Empty;

        private string scannerPortWorker1Line2 = string.Empty;
        private string scannerPortWorker2Line2 = string.Empty;
        private string scannerPortWorker3Line2 = string.Empty;

        private string scannerPortWorker1Line3 = string.Empty;
        private string scannerPortWorker2Line3 = string.Empty;
        private string scannerPortWorker3Line3 = string.Empty;

        private string printerNameWorker1Line1 = string.Empty;
        private string printerNameWorker2Line1 = string.Empty;
        private string printerNameWorker3Line1 = string.Empty;
        private string printerNameWorker1Line2 = string.Empty;
        private string printerNameWorker2Line2 = string.Empty;
        private string printerNameWorker3Line2 = string.Empty;
        private string printerNameWorker1Line3 = string.Empty;
        private string printerNameWorker2Line3 = string.Empty;
        private string printerNameWorker3Line3 = string.Empty;

        private List<Queue<string>> QueueListLine1 = new List<Queue<string>>();
        private List<Queue<string>> QueueListLine2 = new List<Queue<string>>();
        private List<Queue<string>> QueueListLine3 = new List<Queue<string>>();

        private WorkersAndLightsSetupModel modelWorkersAndLightsLine1 = new WorkersAndLightsSetupModel();
        private SebangoAndDNoSetupModel modelSebangoDNoLine1 = new SebangoAndDNoSetupModel();
        private LightAndDNoSetupModel modelLightDNoLine1 = new LightAndDNoSetupModel();
        private WorkersAndLightsSetupModel modelWorkersAndLightsLine2 = new WorkersAndLightsSetupModel();
        private SebangoAndDNoSetupModel modelSebangoDNoLine2 = new SebangoAndDNoSetupModel();
        private LightAndDNoSetupModel modelLightDNoLine2 = new LightAndDNoSetupModel();
        private WorkersAndLightsSetupModel modelWorkersAndLightsLine3 = new WorkersAndLightsSetupModel();
        private SebangoAndDNoSetupModel modelSebangoDNoLine3 = new SebangoAndDNoSetupModel();
        private LightAndDNoSetupModel modelLightDNoLine3 = new LightAndDNoSetupModel();
        private SebangoAndProductBookModel modelSebangoAndProductBook = new SebangoAndProductBookModel();

        private InterfaceUpdateHandler InterfaceUpdateHandle1;
        private InterfaceUpdateHandler LightTurnOffHandle1;
        private InterfaceUpdateHandler InterfaceUpdateHandle2;
        private InterfaceUpdateHandler LightTurnOffHandle2;
        private InterfaceUpdateHandler InterfaceUpdateHandle3;
        private InterfaceUpdateHandler LightTurnOffHandle3;

        private List<string> lightNosWorker1Line1 = new List<string>();
        private List<string> lightNosWorker2Line1 = new List<string>();
        private List<string> lightNosWorker3Line1 = new List<string>();
        private List<string> lightNosWorker1Line2 = new List<string>();
        private List<string> lightNosWorker2Line2 = new List<string>();
        private List<string> lightNosWorker3Line2 = new List<string>();
        private List<string> lightNosWorker1Line3 = new List<string>();
        private List<string> lightNosWorker2Line3 = new List<string>();
        private List<string> lightNosWorker3Line3 = new List<string>();

        private float lblFontSize1 = 8F;
        private float txtFontSize1 = 8F;
        private float lblFontSize2 = 8F;
        private float txtFontSize2 = 8F;
        private float lblFontSize3 = 8F;
        private float txtFontSize3 = 8F;

        private Dictionary<string, string> mapping = new Dictionary<string, string>
        {
            {"1","1"},{"2","2"},{"3","3"},{"4","4"},
            {"5","5"},{"6","6"},{"7","7"},{"8","8"},
            {"9","9"},{"A","10"},{"B","11"},{"C","12"},
            {"D","13"},{"E","14"},{"F","15"},{"G","16"},
            {"H","17"},{"I","18"},{"J","19"},{"K","20"},
            {"L","21"},{"M","22"},{"N","23"},{"O","24"},
            {"P","25"},{"Q","26"},{"R","27"},{"S","28"},
            {"T","29"},{"U","30"},{"V","31"},{"W","32"},
            {"X","33"},{"Y","34"},{"Z","35"},{"-","36"},
            {".","37"},{" ","38"},{"$","39"},{"/","40"},
            {"+","41"},{"%","42"},{"0","0"}
        };

        /// <summary>
        /// Constructor
        /// </summary>
        public DistributionOperationView()
        {
            InitializeComponent();

            this.btnLightEnbale1.Visible = true;
            this.btnLightEnbale2.Visible = true;
            this.btnLightEnbale3.Visible = true;
        }

        /// <summary>
        /// Get Form Width
        /// </summary>
        /// <returns></returns>
        protected override int GetFormWidth()
        {
            return 820;
        }

        /// <summary>
        /// Set Form Title
        /// </summary>
        private void SetFormTitle()
        {
            this.Text = "配送作业";
            this.lblLineName1.Text = ConfigInfo.LineName1;
            this.lblLineName2.Text = ConfigInfo.LineName2;
            this.lblLineName3.Text = ConfigInfo.LineName3;
        }

        /// <summary>
        /// Form Shown
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void DistributionOperationView_Shown(object sender, EventArgs e)
        {
            this.SetFormTitle();
            this.TurnOnLEDGreen();
        }

        /// <summary>
        /// Form Load
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void DistributionOperationView_Load(object sender, EventArgs e)
        {
            this.GetWorkersAndLightsSetup();
            this.InitOperationArea();

            this.InitSebangoAndDNoSetup();
            this.InitLightAndDNoSetup();

            this.InitQueue();

            this.InterfaceUpdateHandle1 = new InterfaceUpdateHandler(UpdateTextBox1);
            this.InterfaceUpdateHandle2 = new InterfaceUpdateHandler(UpdateTextBox2);
            this.InterfaceUpdateHandle3 = new InterfaceUpdateHandler(UpdateTextBox3);

            this.LightTurnOffHandle1 = new InterfaceUpdateHandler(HandleTurnedOffLightNo1);
            this.LightTurnOffHandle2 = new InterfaceUpdateHandler(HandleTurnedOffLightNo2);
            this.LightTurnOffHandle3 = new InterfaceUpdateHandler(HandleTurnedOffLightNo3);

            this.InitAIPlug();

            this.tlpWhole.Dock = DockStyle.Fill;
#if DEBUG
            this.tlpWhole.Dock = DockStyle.Bottom;
            this.lblSebangoTest.Visible = true;
            this.cmbSebangoTest.Visible = true;
            this.cmbSerialPortTest.Visible = true;
            this.btnTest.Visible = true;
            this.btnTest2.Visible = true;
            this.btnClearTest.Visible = true;

            this.cmbSebangoTest.Items.Add("t910111231012341001G");
            this.cmbSebangoTest.Items.Add("t910111232098762020$");
#endif
        }

        private void InitQueue()
        {
            for (int i = 0; i < this.currentWorkerCountLine1; i++)
            {
                QueueListLine1.Add(new Queue<string>());
            }

            for (int i = 0; i < this.currentWorkerCountLine2; i++)
            {
                QueueListLine2.Add(new Queue<string>());
            }

            for (int i = 0; i < this.currentWorkerCountLine3; i++)
            {
                QueueListLine3.Add(new Queue<string>());
            }
        }

        /// <summary>
        /// Get Current WorkersAndLightsSetup Data From Database
        /// </summary>
        private void GetWorkersAndLightsSetup()
        {
            this.modelWorkersAndLightsLine1 = PickingBusiness.Configuration.GetWorkersAndLightsSetup(ConfigInfo.LineCodeA);
            this.currentWorkerCountLine1 = this.modelWorkersAndLightsLine1.WorkersAndLightsSetupTable.Rows.Count;

            this.modelWorkersAndLightsLine2 = PickingBusiness.Configuration.GetWorkersAndLightsSetup(ConfigInfo.LineCodeB);
            this.currentWorkerCountLine2 = this.modelWorkersAndLightsLine2.WorkersAndLightsSetupTable.Rows.Count;

            this.modelWorkersAndLightsLine3 = PickingBusiness.Configuration.GetWorkersAndLightsSetup(ConfigInfo.LineCodeC);
            this.currentWorkerCountLine3 = this.modelWorkersAndLightsLine3.WorkersAndLightsSetupTable.Rows.Count;
        }

        /// <summary>
        /// Init Sebango And DNo
        /// Get Sebango And DNo Setup From Database
        /// </summary>
        private void InitSebangoAndDNoSetup()
        {
            this.modelSebangoDNoLine1 = PickingBusiness.Configuration.GetSebangoAndDNoSetup(ConfigInfo.LineCodeA);
            this.modelSebangoDNoLine2 = PickingBusiness.Configuration.GetSebangoAndDNoSetup(ConfigInfo.LineCodeB);
            this.modelSebangoDNoLine3 = PickingBusiness.Configuration.GetSebangoAndDNoSetup(ConfigInfo.LineCodeC);
        }

        /// <summary>
        /// Init DNo
        /// Get Light And DNo Setup From Database
        /// </summary>
        private void InitLightAndDNoSetup()
        {
            this.modelLightDNoLine1 = PickingBusiness.Configuration.GetLightAndDNoSetup(ConfigInfo.LineCodeA);
            this.modelLightDNoLine2 = PickingBusiness.Configuration.GetLightAndDNoSetup(ConfigInfo.LineCodeB);
            this.modelLightDNoLine3 = PickingBusiness.Configuration.GetLightAndDNoSetup(ConfigInfo.LineCodeC);
        }

        /// <summary>
        /// Init Font
        /// </summary>
        private void InitFont()
        {
            foreach (Control control in this.Controls)
            {
                if (control.Tag != null)
                {
                    this.UpdateFont(control);
                }

                InitFont(control);
            }
        }

        /// <summary>
        /// Init Font
        /// </summary>
        /// <param name="controlParent"></param>
        private void InitFont(Control controlParent)
        {
            foreach (Control control in controlParent.Controls)
            {
                if (control.Tag != null)
                {
                    this.UpdateFont(control);
                }

                InitFont(control);
            }
        }

        /// <summary>
        /// Update Font
        /// </summary>
        /// <param name="control"></param>
        private void UpdateFont(Control control)
        {
            if (control.Tag.ToString().StartsWith("FontSizeCaption1"))
            {
                this.SetFont(control, "SimSun", this.lblFontSize1, FontStyle.Regular);
            }
            else if (control.Tag.ToString().StartsWith("FontSizeValue1"))
            {
                this.SetFont(control, "SimSun", this.txtFontSize1, FontStyle.Regular);
            }
            else if (control.Tag.ToString().StartsWith("FontSizeCaption2"))
            {
                this.SetFont(control, "SimSun", this.lblFontSize2, FontStyle.Regular);
            }
            else if (control.Tag.ToString().StartsWith("FontSizeValue2"))
            {
                this.SetFont(control, "SimSun", this.txtFontSize2, FontStyle.Regular);
            }
            else if (control.Tag.ToString().StartsWith("FontSizeCaption3"))
            {
                this.SetFont(control, "SimSun", this.lblFontSize3, FontStyle.Regular);
            }
            else if (control.Tag.ToString().StartsWith("FontSizeValue3"))
            {
                this.SetFont(control, "SimSun", this.txtFontSize3, FontStyle.Regular);
            }
        }

        /// <summary>
        /// Set Font
        /// </summary>
        /// <param name="control"></param>
        /// <param name="familyName"></param>
        /// <param name="emSize"></param>
        /// <param name="style"></param>
        private void SetFont(Control control, string familyName, float emSize, FontStyle style)
        {
            control.Font = new System.Drawing.Font(familyName,
                                                   emSize,
                                                   style,
                                                   System.Drawing.GraphicsUnit.Point,
                                                   ((byte)(0))
                                                   );
        }

        /// <summary>
        /// Init Operation Area
        /// </summary>
        private void InitOperationArea()
        {
            // Line1
            if (this.currentWorkerCountLine1 <= 0)
            {
                this.tlpMain1.Visible = false;
            }
            else if (this.currentWorkerCountLine1 <= 1)
            {
                this.tlpOperationArea2Line1.Visible = false;
                this.tlpOperationArea3Line1.Visible = false;

                this.tlpMain1.ColumnStyles.Clear();
                this.tlpMain1.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Absolute, 10F));
                this.tlpMain1.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 100F));
                this.tlpMain1.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Absolute, 0F));
                this.tlpMain1.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Absolute, 0F));
                this.tlpMain1.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Absolute, 0F));
                this.tlpMain1.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Absolute, 10F));
                // Set Font Size
                this.lblFontSize1 = 20F;
                this.txtFontSize1 = 20F;
            }
            else if (this.currentWorkerCountLine1 <= 2)
            {
                this.tlpOperationArea3Line1.Visible = false;

                this.tlpMain1.ColumnStyles.Clear();
                this.tlpMain1.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Absolute, 10F));
                this.tlpMain1.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 50F));
                this.tlpMain1.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Absolute, 0F));
                this.tlpMain1.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 50F));
                this.tlpMain1.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Absolute, 0F));
                this.tlpMain1.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Absolute, 10F));
                // Set Font Size
                this.lblFontSize1 = 12F;
                this.txtFontSize1 = 12F;
            }
            else if (this.currentWorkerCountLine1 <= 3)
            {
                this.tlpMain1.ColumnStyles.Clear();
                this.tlpMain1.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Absolute, 10F));
                this.tlpMain1.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 30F));
                this.tlpMain1.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Absolute, 0F));
                this.tlpMain1.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 30F));
                this.tlpMain1.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 30F));
                this.tlpMain1.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Absolute, 10F));
                // Set Font Size
                this.lblFontSize1 = 8F;
                this.txtFontSize1 = 8F;
            }

            // Line2
            if (this.currentWorkerCountLine2 <= 0)
            {
                this.tlpMain2.Visible = false;
            }
            else if (this.currentWorkerCountLine2 <= 1)
            {
                this.tlpOperationArea2Line2.Visible = false;
                this.tlpOperationArea3Line2.Visible = false;

                this.tlpMain2.ColumnStyles.Clear();
                this.tlpMain2.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Absolute, 10F));
                this.tlpMain2.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 100F));
                this.tlpMain2.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Absolute, 0F));
                this.tlpMain2.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Absolute, 0F));
                this.tlpMain2.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Absolute, 0F));
                this.tlpMain2.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Absolute, 10F));
                // Set Font Size
                this.lblFontSize2 = 20F;
                this.txtFontSize2 = 20F;
            }
            else if (this.currentWorkerCountLine2 <= 2)
            {
                this.tlpOperationArea3Line2.Visible = false;

                this.tlpMain2.ColumnStyles.Clear();
                this.tlpMain2.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Absolute, 10F));
                this.tlpMain2.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 50F));
                this.tlpMain2.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Absolute, 0F));
                this.tlpMain2.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 50F));
                this.tlpMain2.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Absolute, 0F));
                this.tlpMain2.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Absolute, 10F));
                // Set Font Size
                this.lblFontSize2 = 12F;
                this.txtFontSize2 = 12F;
            }
            else if (this.currentWorkerCountLine2 <= 3)
            {
                this.tlpMain2.ColumnStyles.Clear();
                this.tlpMain2.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Absolute, 10F));
                this.tlpMain2.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 30F));
                this.tlpMain2.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Absolute, 0F));
                this.tlpMain2.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 30F));
                this.tlpMain2.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 30F));
                this.tlpMain2.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Absolute, 10F));
                // Set Font Size
                this.lblFontSize2 = 8F;
                this.txtFontSize2 = 8F;
            }

            // Line3
            if (this.currentWorkerCountLine3 <= 0)
            {
                this.tlpMain3.Visible = false;
            }
            else if (this.currentWorkerCountLine3 <= 1)
            {
                this.tlpOperationArea2Line3.Visible = false;
                this.tlpOperationArea3Line3.Visible = false;

                this.tlpMain3.ColumnStyles.Clear();
                this.tlpMain3.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Absolute, 10F));
                this.tlpMain3.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 100F));
                this.tlpMain3.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Absolute, 0F));
                this.tlpMain3.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Absolute, 0F));
                this.tlpMain3.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Absolute, 0F));
                this.tlpMain3.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Absolute, 10F));
                // Set Font Size
                this.lblFontSize3 = 20F;
                this.txtFontSize3 = 20F;
            }
            else if (this.currentWorkerCountLine3 <= 2)
            {
                this.tlpOperationArea3Line3.Visible = false;

                this.tlpMain3.ColumnStyles.Clear();
                this.tlpMain3.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Absolute, 10F));
                this.tlpMain3.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 50F));
                this.tlpMain3.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Absolute, 0F));
                this.tlpMain3.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 50F));
                this.tlpMain3.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Absolute, 0F));
                this.tlpMain3.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Absolute, 10F));
                // Set Font Size
                this.lblFontSize3 = 12F;
                this.txtFontSize3 = 12F;
            }
            else if (this.currentWorkerCountLine3 <= 3)
            {
                this.tlpMain3.ColumnStyles.Clear();
                this.tlpMain3.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Absolute, 10F));
                this.tlpMain3.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 30F));
                this.tlpMain3.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Absolute, 0F));
                this.tlpMain3.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 30F));
                this.tlpMain3.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 30F));
                this.tlpMain3.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Absolute, 10F));
                // Set Font Size
                this.lblFontSize3 = 8F;
                this.txtFontSize3 = 8F;
            }

            // Set Font
            this.InitFont();
        }

        /// <summary>
        /// Init AIPlug
        /// </summary>
        private void InitAIPlug()
        {
            if (!string.IsNullOrWhiteSpace(ConfigInfo.AxAiPlugIpAddress1))
            {
                this.scannerPortWorker1Line1 = ConfigurationManager.AppSettings["ScanGunPortWorker1Line1"].ToString().Trim();
                this.scannerPortWorker2Line1 = ConfigurationManager.AppSettings["ScanGunPortWorker2Line1"].ToString().Trim();
                this.scannerPortWorker3Line1 = ConfigurationManager.AppSettings["ScanGunPortWorker3Line1"].ToString().Trim();

                this.printerNameWorker1Line1 = ConfigurationManager.AppSettings["PrinterNameWorker1Line1"].ToString().Trim();
                this.printerNameWorker2Line1 = ConfigurationManager.AppSettings["PrinterNameWorker2Line1"].ToString().Trim();
                this.printerNameWorker3Line1 = ConfigurationManager.AppSettings["PrinterNameWorker3Line1"].ToString().Trim();

                axAiPlug1.ConnectSocket(ConfigInfo.AxAiPlugIpAddress1, ConfigInfo.AxAiPlugPort1);

                axAiPlug1.StartPolling(); //watch the data arrival

                axAiPlug1.BinaryMode = true; //set to true

                axAiPlug1.CommandTimeout = 5000; //set to 5000

                this.axAiPlug1.DataArrival
                    += new AxAiPlugControl.@__AiPlug_DataArrivalEventHandler(this.axAiPlug1_DataArrival);

                this.OpenAllSanner(ConfigInfo.LineCodeA);
            }

            if (!string.IsNullOrWhiteSpace(ConfigInfo.AxAiPlugIpAddress2))
            {
                this.scannerPortWorker1Line2 = ConfigurationManager.AppSettings["ScanGunPortWorker1Line2"].ToString().Trim();
                this.scannerPortWorker2Line2 = ConfigurationManager.AppSettings["ScanGunPortWorker2Line2"].ToString().Trim();
                this.scannerPortWorker3Line2 = ConfigurationManager.AppSettings["ScanGunPortWorker3Line2"].ToString().Trim();

                this.printerNameWorker1Line2 = ConfigurationManager.AppSettings["PrinterNameWorker1Line2"].ToString().Trim();
                this.printerNameWorker2Line2 = ConfigurationManager.AppSettings["PrinterNameWorker2Line2"].ToString().Trim();
                this.printerNameWorker3Line2 = ConfigurationManager.AppSettings["PrinterNameWorker3Line2"].ToString().Trim();

                axAiPlug2.ConnectSocket(ConfigInfo.AxAiPlugIpAddress2, ConfigInfo.AxAiPlugPort2);

                axAiPlug2.StartPolling(); //watch the data arrival

                axAiPlug2.BinaryMode = true; //set to true

                axAiPlug2.CommandTimeout = 5000; //set to 5000

                this.axAiPlug2.DataArrival
                    += new AxAiPlugControl.@__AiPlug_DataArrivalEventHandler(this.axAiPlug2_DataArrival);

                this.OpenAllSanner(ConfigInfo.LineCodeB);
            }

            if (!string.IsNullOrWhiteSpace(ConfigInfo.AxAiPlugIpAddress3))
            {
                this.scannerPortWorker1Line3 = ConfigurationManager.AppSettings["ScanGunPortWorker1Line3"].ToString().Trim();
                this.scannerPortWorker2Line3 = ConfigurationManager.AppSettings["ScanGunPortWorker2Line3"].ToString().Trim();
                this.scannerPortWorker3Line3 = ConfigurationManager.AppSettings["ScanGunPortWorker3Line3"].ToString().Trim();

                this.printerNameWorker1Line3 = ConfigurationManager.AppSettings["PrinterNameWorker1Line3"].ToString().Trim();
                this.printerNameWorker2Line3 = ConfigurationManager.AppSettings["PrinterNameWorker2Line3"].ToString().Trim();
                this.printerNameWorker3Line3 = ConfigurationManager.AppSettings["PrinterNameWorker3Line3"].ToString().Trim();

                axAiPlug3.ConnectSocket(ConfigInfo.AxAiPlugIpAddress3, ConfigInfo.AxAiPlugPort3);

                axAiPlug3.StartPolling(); //watch the data arrival

                axAiPlug3.BinaryMode = true; //set to true

                axAiPlug3.CommandTimeout = 5000; //set to 5000

                this.axAiPlug3.DataArrival
                    += new AxAiPlugControl.@__AiPlug_DataArrivalEventHandler(this.axAiPlug3_DataArrival);

                this.OpenAllSanner(ConfigInfo.LineCodeC);
            }
        }

        /// <summary>
        /// Open All Sanner
        /// </summary>
        private void OpenAllSanner(string lineCode)
        {
            // Line1
            if (string.Equals(ConfigInfo.LineCodeA, lineCode))
            {
                if (!string.IsNullOrWhiteSpace(this.scannerPortWorker1Line1))
                {
                    axAiPlug1.SendCommand("G" + this.scannerPortWorker1Line1 + "o");
                    this.scannerEnbaleLine1 = true;
                }

                if (!string.IsNullOrWhiteSpace(this.scannerPortWorker2Line1))
                {
                    axAiPlug1.SendCommand("G" + this.scannerPortWorker2Line1 + "o");
                    this.scannerEnbaleLine1 = true;
                }

                if (!string.IsNullOrWhiteSpace(this.scannerPortWorker3Line1))
                {
                    axAiPlug1.SendCommand("G" + this.scannerPortWorker3Line1 + "o");
                    this.scannerEnbaleLine1 = true;
                }
            }

            // Line2
            if (string.Equals(ConfigInfo.LineCodeB, lineCode))
            {
                if (!string.IsNullOrWhiteSpace(this.scannerPortWorker1Line2))
                {
                    axAiPlug2.SendCommand("G" + this.scannerPortWorker1Line2 + "o");
                    this.scannerEnbaleLine2 = true;
                }

                if (!string.IsNullOrWhiteSpace(this.scannerPortWorker2Line2))
                {
                    axAiPlug2.SendCommand("G" + this.scannerPortWorker2Line2 + "o");
                    this.scannerEnbaleLine2 = true;
                }

                if (!string.IsNullOrWhiteSpace(this.scannerPortWorker3Line2))
                {
                    axAiPlug2.SendCommand("G" + this.scannerPortWorker3Line2 + "o");
                    this.scannerEnbaleLine2 = true;
                }
            }

            // Line3
            if (string.Equals(ConfigInfo.LineCodeC, lineCode))
            {
                if (!string.IsNullOrWhiteSpace(this.scannerPortWorker1Line3))
                {
                    axAiPlug3.SendCommand("G" + this.scannerPortWorker1Line3 + "o");
                    this.scannerEnbaleLine3 = true;
                }

                if (!string.IsNullOrWhiteSpace(this.scannerPortWorker2Line3))
                {
                    axAiPlug3.SendCommand("G" + this.scannerPortWorker2Line3 + "o");
                    this.scannerEnbaleLine3 = true;
                }

                if (!string.IsNullOrWhiteSpace(this.scannerPortWorker3Line3))
                {
                    axAiPlug3.SendCommand("G" + this.scannerPortWorker3Line3 + "o");
                    this.scannerEnbaleLine3 = true;
                }
            }
        }

        /// <summary>
        /// Close All Sanner
        /// </summary>
        private void CloseAllSanner(string lineCode)
        {
            // Line1
            if (string.Equals(ConfigInfo.LineCodeA, lineCode))
            {
                if (!string.IsNullOrWhiteSpace(this.scannerPortWorker1Line1))
                {
                    axAiPlug1.SendCommand("G" + this.scannerPortWorker1Line1 + "c");
                }

                if (!string.IsNullOrWhiteSpace(this.scannerPortWorker2Line1))
                {
                    axAiPlug1.SendCommand("G" + this.scannerPortWorker2Line1 + "c");
                }

                if (!string.IsNullOrWhiteSpace(this.scannerPortWorker3Line1))
                {
                    axAiPlug1.SendCommand("G" + this.scannerPortWorker3Line1 + "c");
                }

                this.scannerEnbaleLine1 = false;
            }

            // Line2
            if (string.Equals(ConfigInfo.LineCodeB, lineCode))
            {
                if (!string.IsNullOrWhiteSpace(this.scannerPortWorker1Line2))
                {
                    axAiPlug2.SendCommand("G" + this.scannerPortWorker1Line2 + "c");
                }

                if (!string.IsNullOrWhiteSpace(this.scannerPortWorker2Line2))
                {
                    axAiPlug2.SendCommand("G" + this.scannerPortWorker2Line2 + "c");
                }

                if (!string.IsNullOrWhiteSpace(this.scannerPortWorker3Line2))
                {
                    axAiPlug2.SendCommand("G" + this.scannerPortWorker3Line2 + "c");
                }

                this.scannerEnbaleLine2 = false;
            }

            // Line3
            if (string.Equals(ConfigInfo.LineCodeC, lineCode))
            {
                if (!string.IsNullOrWhiteSpace(this.scannerPortWorker1Line3))
                {
                    axAiPlug3.SendCommand("G" + this.scannerPortWorker1Line3 + "c");
                }

                if (!string.IsNullOrWhiteSpace(this.scannerPortWorker2Line3))
                {
                    axAiPlug3.SendCommand("G" + this.scannerPortWorker2Line3 + "c");
                }

                if (!string.IsNullOrWhiteSpace(this.scannerPortWorker3Line3))
                {
                    axAiPlug3.SendCommand("G" + this.scannerPortWorker3Line3 + "c");
                }

                this.scannerEnbaleLine3 = false;
            }
        }

        /// <summary>
        /// Is No Remain Piece Line1
        /// </summary>
        /// <param name="port"></param>
        /// <returns></returns>
        private bool IsAllNoRemainPieceLine1()
        {
            if ((string.IsNullOrEmpty(this.txtRemainPiece1Line1.Text.Trim())
                    || string.Equals(this.txtRemainPiece1Line1.Text.Trim(), "0"))
                    && (string.IsNullOrEmpty(this.txtRemainPiece2Line1.Text.Trim())
                    || string.Equals(this.txtRemainPiece2Line1.Text.Trim(), "0"))
                    && (string.IsNullOrEmpty(this.txtRemainPiece3Line1.Text.Trim())
                    || string.Equals(this.txtRemainPiece3Line1.Text.Trim(), "0")))
            {
                return true;
            }

            return false;
        }

        /// <summary>
        /// Is No Remain Piece Line2
        /// </summary>
        /// <param name="port"></param>
        /// <returns></returns>
        private bool IsAllNoRemainPieceLine2()
        {
            if ((string.IsNullOrEmpty(this.txtRemainPiece1Line2.Text.Trim())
                    || string.Equals(this.txtRemainPiece1Line2.Text.Trim(), "0"))
                    && (string.IsNullOrEmpty(this.txtRemainPiece2Line2.Text.Trim())
                    || string.Equals(this.txtRemainPiece2Line2.Text.Trim(), "0"))
                    && (string.IsNullOrEmpty(this.txtRemainPiece3Line2.Text.Trim())
                    || string.Equals(this.txtRemainPiece3Line2.Text.Trim(), "0")))
            {
                return true;
            }

            return false;
        }

        /// <summary>
        /// Is No Remain Piece Line3
        /// </summary>
        /// <param name="port"></param>
        /// <returns></returns>
        private bool IsAllNoRemainPieceLine3()
        {
            if ((string.IsNullOrEmpty(this.txtRemainPiece1Line3.Text.Trim())
                    || string.Equals(this.txtRemainPiece1Line3.Text.Trim(), "0"))
                    && (string.IsNullOrEmpty(this.txtRemainPiece2Line3.Text.Trim())
                    || string.Equals(this.txtRemainPiece2Line3.Text.Trim(), "0"))
                    && (string.IsNullOrEmpty(this.txtRemainPiece3Line3.Text.Trim())
                    || string.Equals(this.txtRemainPiece3Line3.Text.Trim(), "0")))
            {
                return true;
            }

            return false;
        }

        //private void TryToTurnOnLights(object context)
        //{
        //    while (true)
        //    {
        //        if (mre.WaitOne())
        //        {
        //            if (this.currentWorkerCount == 2)
        //            {
        //                if (this.lightNosWorker2.Count == 0 && this.SebangoListB.Count > 0)
        //                {
        //                    string sebango = this.SebangoListB.Dequeue();
        //                    SynchronizationContext con = context as SynchronizationContext;
        //                    con.Send(p =>
        //                    {
        //                        this.txtSebango2.Text = sebango;
        //                        this.lightNosWorker2.Clear();
        //                        this.DisplayLightNo(this.ltvLight2, this.txtSebango2.Text.Trim(), 2);
        //                        if (this.ltvLight2.Items.Count > 0) this.TurnOnLight(this.lightNosWorker2);
        //                    }, null);
        //                    mre.Reset();
        //                }
        //            }
        //            else if (this.currentWorkerCount == 3)
        //            {
        //                SynchronizationContext con = context as SynchronizationContext;

        //                int currentRemainPiece1 = 0;
        //                string remainPiece1 = this.txtRemainPiece1.Text.Trim();
        //                int.TryParse(remainPiece1, out currentRemainPiece1);

        //                int currentRemainPiece2 = 0;
        //                string remainPiece2 = this.txtRemainPiece2.Text.Trim();
        //                int.TryParse(remainPiece2, out currentRemainPiece2);

        //                if ((currentRemainPiece1 == 0)
        //                    && (currentRemainPiece2 != 0))
        //                {
        //                    con.Send(p =>
        //                    {
        //                        this.lightNosWorker2.Clear();
        //                        this.DisplayLightNo(this.ltvLight2, this.txtSebango2.Text.Trim(), 2);
        //                        if (this.ltvLight2.Items.Count > 0) this.TurnOnLight(this.lightNosWorker2);
        //                    }, null);
        //                    mre.Reset();
        //                }
        //                else if (currentRemainPiece2 == 0)
        //                {
        //                    con.Send(p =>
        //                    {
        //                        this.lightNosWorker3.Clear();
        //                        this.DisplayLightNo(this.ltvLight3, this.txtSebango3.Text.Trim(), 3);
        //                        if (this.ltvLight3.Items.Count > 0) this.TurnOnLight(this.lightNosWorker3);
        //                    }, null);
        //                    break;
        //                }
        //            }
        //        }
        //        else
        //        {
        //            Thread.Sleep(1000);
        //        }
        //    }
        //}

        /// <summary>
        /// Update Text Box 1
        /// </summary>
        /// <param name="text"></param>
        /// <param name="portName"></param>
        private void UpdateTextBox1(string text, string port)
        {
            if (this.currentWorkerCountLine1 <= 0)
            {
                return;
            }

            string productName = this.GetProductName(text.Trim());
            string sebango = this.GetSebango(text.Trim());
            string remainPiece = this.GetRemainPiece(text.Trim());
            string sequence = this.GetSequence(text.Trim());

            int remainPieceNumber = 0;
            int.TryParse(remainPiece, out remainPieceNumber);

            //这个方法是在扫描枪扫描的时候执行的
            //当一个条码被扫描之后，将这个条码保存到所有打开的流水线的队列里面去
            if (this.lightEnbaleLine1)
            {
                for (int i = 0; i < this.currentWorkerCountLine1; i++)
                {
                    Queue<string> q = QueueListLine1[i];
                    q.Enqueue(text);
                }
            }

            if (this.currentWorkerCountLine1 <= 1)
            {
                this.txtProduct1Line1.Text = productName;
                this.txtSebango1Line1.Text = sebango;
                this.txtRemainPiece1Line1.Text = remainPiece;

                if (this.lightEnbaleLine1)
                {
                    if (remainPieceNumber > 0)
                    {
                        this.lightNosWorker1Line1.Clear();
                        this.DisplayLightNo1(this.ltvLight1Line1, this.txtSebango1Line1.Text.Trim(), 1);

                        if (this.ltvLight1Line1.Items.Count > 0)
                        {
                            this.TurnOnLight1(this.lightNosWorker1Line1);
                        }
                        else
                        {
                            this.txtRemainPiece1Line1.Text = "0";
                        }
                    }
                }

                if (string.Equals(ConfigInfo.IsPrint1.ToUpper(), "T"))
                {
                    this.Print(this.printerNameWorker1Line1, ConfigInfo.LineName1, ConfigInfo.LineCode1,
                               sebango, remainPiece, sequence, productName, 0);
                }
            }
            else if (this.currentWorkerCountLine1 <= 2)
            {
                this.txtProduct1Line1.Text = productName;
                this.txtSebango1Line1.Text = sebango;
                this.txtRemainPiece1Line1.Text = remainPiece;

                if (this.IsRemainPieceZero(this.txtRemainPiece2Line1.Text.Trim()))
                {
                    this.txtProduct2Line1.Text = productName;
                    this.txtSebango2Line1.Text = sebango;
                    this.txtRemainPiece2Line1.Text = remainPiece;
                }

                if (this.lightEnbaleLine1)
                {
                    if (remainPieceNumber > 0)
                    {
                        this.lightNosWorker1Line1.Clear();
                        this.DisplayLightNo1(this.ltvLight1Line1, this.txtSebango1Line1.Text.Trim(), 1);

                        ////this.lightNosWorker2Line1.Clear();
                        ////this.DisplayLightNo1(this.ltvLight2Line1, this.txtSebango2Line1.Text.Trim(), 2);

                        if (this.ltvLight1Line1.Items.Count > 0)
                        {
                            this.TurnOnLight1(this.lightNosWorker2Line1);
                        }
                        else
                        {
                            this.txtRemainPiece1Line1.Text = "0";
                        }

                        ////if (this.ltvLight2Line1.Items.Count > 0)
                        ////{
                        ////    this.TurnOnLight1(this.lightNosWorker2Line1);
                        ////}
                        ////else
                        ////{
                        ////    this.txtRemainPiece2Line1.Text = "0";
                        ////}
                    }
                }

                if (string.Equals(ConfigInfo.IsPrint1.ToUpper(), "T"))
                {
                    this.Print(this.printerNameWorker1Line1, ConfigInfo.LineName1, ConfigInfo.LineCode1,
                               sebango, remainPiece, sequence, productName, 0);

                    this.Print(this.printerNameWorker2Line1, ConfigInfo.LineName1, ConfigInfo.LineCode1,
                               sebango, remainPiece, sequence, productName, 0);
                }
            }
            else
            {
                this.txtProduct1Line1.Text = productName;
                this.txtSebango1Line1.Text = sebango;
                this.txtRemainPiece1Line1.Text = remainPiece;

                if (this.IsRemainPieceZero(this.txtRemainPiece2Line1.Text.Trim()))
                {
                    this.txtProduct2Line1.Text = productName;
                    this.txtSebango2Line1.Text = sebango;
                    this.txtRemainPiece2Line1.Text = remainPiece;
                }

                if (this.IsRemainPieceZero(this.txtRemainPiece3Line1.Text.Trim()))
                {
                    this.txtProduct3Line1.Text = productName;
                    this.txtSebango3Line1.Text = sebango;
                    this.txtRemainPiece3Line1.Text = remainPiece;
                }

                if (this.lightEnbaleLine1)
                {
                    if (remainPieceNumber > 0)
                    {
                        this.lightNosWorker1Line1.Clear();
                        this.DisplayLightNo1(this.ltvLight1Line1, this.txtSebango1Line1.Text.Trim(), 1);

                        ////this.lightNosWorker2Line1.Clear();
                        ////this.DisplayLightNo1(this.ltvLight2Line1, this.txtSebango2Line1.Text.Trim(), 2);

                        ////this.lightNosWorker3Line1.Clear();
                        ////this.DisplayLightNo1(this.ltvLight3Line1, this.txtSebango3Line1.Text.Trim(), 3);

                        if (this.ltvLight1Line1.Items.Count > 0)
                        {
                            this.TurnOnLight1(this.lightNosWorker3Line1);
                        }
                        else
                        {
                            this.txtRemainPiece1Line1.Text = "0";
                        }

                        ////if (this.ltvLight2Line1.Items.Count > 0)
                        ////{
                        ////    this.TurnOnLight1(this.lightNosWorker2Line1);
                        ////}
                        ////else
                        ////{
                        ////    this.txtRemainPiece2Line1.Text = "0";
                        ////}

                        ////if (this.ltvLight3Line1.Items.Count > 0)
                        ////{
                        ////    this.TurnOnLight1(this.lightNosWorker3Line1);
                        ////}
                        ////else
                        ////{
                        ////    this.txtRemainPiece3Line1.Text = "0";
                        ////}
                    }
                }

                if (string.Equals(ConfigInfo.IsPrint1.ToUpper(), "T"))
                {
                    this.Print(this.printerNameWorker1Line1, ConfigInfo.LineName1, ConfigInfo.LineCode1,
                               sebango, remainPiece, sequence, productName, 0);

                    this.Print(this.printerNameWorker2Line1, ConfigInfo.LineName1, ConfigInfo.LineCode1,
                               sebango, remainPiece, sequence, productName, 0);

                    this.Print(this.printerNameWorker3Line1, ConfigInfo.LineName1, ConfigInfo.LineCode1,
                               sebango, remainPiece, sequence, productName, 0);
                }
            }
        }


        /// <summary>
        /// Update Text Box 2
        /// </summary>
        /// <param name="text"></param>
        /// <param name="portName"></param>
        private void UpdateTextBox2(string text, string port)
        {
            if (this.currentWorkerCountLine2 <= 0)
            {
                return;
            }

            string productName = this.GetProductName(text.Trim());
            string sebango = this.GetSebango(text.Trim());
            string remainPiece = this.GetRemainPiece(text.Trim());
            string sequence = this.GetSequence(text.Trim());

            int remainPieceNumber = 0;
            int.TryParse(remainPiece, out remainPieceNumber);

            //这个方法是在扫描枪扫描的时候执行的
            //当一个条码被扫描之后，将这个条码保存到所有打开的流水线的队列里面去
            if (this.lightEnbaleLine2)
            {
                for (int i = 0; i < this.currentWorkerCountLine2; i++)
                {
                    Queue<string> q = QueueListLine2[i];
                    q.Enqueue(text);
                }
            }

            if (this.currentWorkerCountLine2 <= 1)
            {
                this.txtProduct1Line2.Text = productName;
                this.txtSebango1Line2.Text = sebango;
                this.txtRemainPiece1Line2.Text = remainPiece;

                if (this.lightEnbaleLine2)
                {
                    if (remainPieceNumber > 0)
                    {
                        this.lightNosWorker2Line2.Clear();
                        this.DisplayLightNo2(this.ltvLight1Line2, this.txtSebango1Line2.Text.Trim(), 1);

                        if (this.ltvLight1Line2.Items.Count > 0)
                        {
                            this.TurnOnLight2(this.lightNosWorker1Line2);
                        }
                        else
                        {
                            this.txtRemainPiece1Line2.Text = "0";
                        }
                    }
                }

                if (string.Equals(ConfigInfo.IsPrint2.ToUpper(), "T"))
                {
                    this.Print(this.printerNameWorker1Line2, ConfigInfo.LineName2, ConfigInfo.LineCode2,
                               sebango, remainPiece, sequence, productName, 1);
                }
            }
            else if (this.currentWorkerCountLine2 <= 2)
            {
                this.txtProduct1Line2.Text = productName;
                this.txtSebango1Line2.Text = sebango;
                this.txtRemainPiece1Line2.Text = remainPiece;

                if (this.IsRemainPieceZero(this.txtRemainPiece2Line2.Text.Trim()))
                {
                    this.txtProduct2Line2.Text = productName;
                    this.txtSebango2Line2.Text = sebango;
                    this.txtRemainPiece2Line2.Text = remainPiece;
                }

                if (this.lightEnbaleLine2)
                {
                    if (remainPieceNumber > 0)
                    {
                        this.lightNosWorker1Line2.Clear();
                        this.DisplayLightNo2(this.ltvLight1Line2, this.txtSebango1Line2.Text.Trim(), 1);

                        ////this.lightNosWorker2Line2.Clear();
                        ////this.DisplayLightNo2(this.ltvLight2Line2, this.txtSebango2Line2.Text.Trim(), 2);

                        if (this.ltvLight1Line2.Items.Count > 0)
                        {
                            this.TurnOnLight2(this.lightNosWorker2Line2);
                        }
                        else
                        {
                            this.txtRemainPiece1Line2.Text = "0";
                        }

                        ////if (this.ltvLight2Line2.Items.Count > 0)
                        ////{
                        ////    this.TurnOnLight2(this.lightNosWorker2Line2);
                        ////}
                        ////else
                        ////{
                        ////    this.txtRemainPiece2Line2.Text = "0";
                        ////}
                    }
                }

                if (string.Equals(ConfigInfo.IsPrint2.ToUpper(), "T"))
                {
                    this.Print(this.printerNameWorker1Line2, ConfigInfo.LineName2, ConfigInfo.LineCode2,
                               sebango, remainPiece, sequence, productName, 1);

                    this.Print(this.printerNameWorker2Line2, ConfigInfo.LineName2, ConfigInfo.LineCode2,
                               sebango, remainPiece, sequence, productName, 1);
                }
            }
            else
            {
                this.txtProduct1Line2.Text = productName;
                this.txtSebango1Line2.Text = sebango;
                this.txtRemainPiece1Line2.Text = remainPiece;

                if (this.IsRemainPieceZero(this.txtRemainPiece2Line2.Text.Trim()))
                {
                    this.txtProduct2Line2.Text = productName;
                    this.txtSebango2Line2.Text = sebango;
                    this.txtRemainPiece2Line2.Text = remainPiece;
                }

                if (this.IsRemainPieceZero(this.txtRemainPiece3Line2.Text.Trim()))
                {
                    this.txtProduct3Line2.Text = productName;
                    this.txtSebango3Line2.Text = sebango;
                    this.txtRemainPiece3Line2.Text = remainPiece;
                }

                if (this.lightEnbaleLine2)
                {
                    if (remainPieceNumber > 0)
                    {
                        this.lightNosWorker1Line2.Clear();
                        this.DisplayLightNo2(this.ltvLight1Line2, this.txtSebango1Line2.Text.Trim(), 1);

                        ////this.lightNosWorker2Line2.Clear();
                        ////this.DisplayLightNo2(this.ltvLight2Line2, this.txtSebango2Line2.Text.Trim(), 2);

                        ////this.lightNosWorker3Line2.Clear();
                        ////this.DisplayLightNo2(this.ltvLight3Line2, this.txtSebango3Line2.Text.Trim(), 3);

                        if (this.ltvLight1Line2.Items.Count > 0)
                        {
                            this.TurnOnLight2(this.lightNosWorker3Line2);
                        }
                        else
                        {
                            this.txtRemainPiece1Line2.Text = "0";
                        }

                        ////if (this.ltvLight2Line2.Items.Count > 0)
                        ////{
                        ////    this.TurnOnLight2(this.lightNosWorker2Line2);
                        ////}
                        ////else
                        ////{
                        ////    this.txtRemainPiece2Line2.Text = "0";
                        ////}

                        ////if (this.ltvLight3Line2.Items.Count > 0)
                        ////{
                        ////    this.TurnOnLight2(this.lightNosWorker3Line2);
                        ////}
                        ////else
                        ////{
                        ////    this.txtRemainPiece3Line2.Text = "0";
                        ////}
                    }
                }

                if (string.Equals(ConfigInfo.IsPrint2.ToUpper(), "T"))
                {
                    this.Print(this.printerNameWorker1Line2, ConfigInfo.LineName2, ConfigInfo.LineCode2,
                               sebango, remainPiece, sequence, productName, 1);

                    this.Print(this.printerNameWorker2Line2, ConfigInfo.LineName2, ConfigInfo.LineCode2,
                               sebango, remainPiece, sequence, productName, 1);

                    this.Print(this.printerNameWorker3Line2, ConfigInfo.LineName2, ConfigInfo.LineCode2,
                               sebango, remainPiece, sequence, productName, 1);
                }
            }
        }

        /// <summary>
        /// Update Text Box 3
        /// </summary>
        /// <param name="text"></param>
        /// <param name="portName"></param>
        private void UpdateTextBox3(string text, string port)
        {
            if (this.currentWorkerCountLine3 <= 0)
            {
                return;
            }

            string productName = this.GetProductName(text.Trim());
            string sebango = this.GetSebango(text.Trim());
            string remainPiece = this.GetRemainPiece(text.Trim());
            string sequence = this.GetSequence(text.Trim());

            int remainPieceNumber = 0;
            int.TryParse(remainPiece, out remainPieceNumber);

            //这个方法是在扫描枪扫描的时候执行的
            //当一个条码被扫描之后，将这个条码保存到所有打开的流水线的队列里面去
            if (this.lightEnbaleLine3)
            {
                for (int i = 0; i < this.currentWorkerCountLine3; i++)
                {
                    Queue<string> q = QueueListLine3[i];
                    q.Enqueue(text);
                }
            }

            if (this.currentWorkerCountLine3 <= 1)
            {
                this.txtProduct1Line3.Text = productName;
                this.txtSebango1Line3.Text = sebango;
                this.txtRemainPiece1Line3.Text = remainPiece;

                if (this.lightEnbaleLine3)
                {
                    if (remainPieceNumber > 0)
                    {
                        this.lightNosWorker2Line3.Clear();
                        this.DisplayLightNo3(this.ltvLight1Line3, this.txtSebango1Line3.Text.Trim(), 1);

                        if (this.ltvLight1Line3.Items.Count > 0)
                        {
                            this.TurnOnLight3(this.lightNosWorker1Line3);
                        }
                        else
                        {
                            this.txtRemainPiece1Line3.Text = "0";
                        }
                    }
                }

                if (string.Equals(ConfigInfo.IsPrint3.ToUpper(), "T"))
                {
                    this.Print(this.printerNameWorker1Line3, ConfigInfo.LineName3, ConfigInfo.LineCode3,
                               sebango, remainPiece, sequence, productName, 2);
                }
            }
            else if (this.currentWorkerCountLine3 <= 2)
            {
                this.txtProduct1Line3.Text = productName;
                this.txtSebango1Line3.Text = sebango;
                this.txtRemainPiece1Line3.Text = remainPiece;

                if (this.IsRemainPieceZero(this.txtRemainPiece2Line3.Text.Trim()))
                {
                    this.txtProduct2Line3.Text = productName;
                    this.txtSebango2Line3.Text = sebango;
                    this.txtRemainPiece2Line3.Text = remainPiece;
                }

                if (this.lightEnbaleLine3)
                {
                    if (remainPieceNumber > 0)
                    {
                        this.lightNosWorker1Line3.Clear();
                        this.DisplayLightNo3(this.ltvLight1Line3, this.txtSebango1Line3.Text.Trim(), 1);

                        ////this.lightNosWorker2Line3.Clear();
                        ////this.DisplayLightNo3(this.ltvLight2Line3, this.txtSebango2Line3.Text.Trim(), 2);

                        if (this.ltvLight1Line3.Items.Count > 0)
                        {
                            this.TurnOnLight3(this.lightNosWorker2Line3);
                        }
                        else
                        {
                            this.txtRemainPiece1Line3.Text = "0";
                        }

                        ////if (this.ltvLight2Line3.Items.Count > 0)
                        ////{
                        ////    this.TurnOnLight3(this.lightNosWorker2Line3);
                        ////}
                        ////else
                        ////{
                        ////    this.txtRemainPiece2Line3.Text = "0";
                        ////}
                    }
                }

                if (string.Equals(ConfigInfo.IsPrint3.ToUpper(), "T"))
                {
                    this.Print(this.printerNameWorker1Line3, ConfigInfo.LineName3, ConfigInfo.LineCode3,
                               sebango, remainPiece, sequence, productName, 2);

                    this.Print(this.printerNameWorker2Line3, ConfigInfo.LineName3, ConfigInfo.LineCode3,
                               sebango, remainPiece, sequence, productName, 2);
                }
            }
            else
            {
                this.txtProduct1Line3.Text = productName;
                this.txtSebango1Line3.Text = sebango;
                this.txtRemainPiece1Line3.Text = remainPiece;

                if (this.IsRemainPieceZero(this.txtRemainPiece2Line3.Text.Trim()))
                {
                    this.txtProduct2Line3.Text = productName;
                    this.txtSebango2Line3.Text = sebango;
                    this.txtRemainPiece2Line3.Text = remainPiece;
                }

                if (this.IsRemainPieceZero(this.txtRemainPiece3Line3.Text.Trim()))
                {
                    this.txtProduct3Line3.Text = productName;
                    this.txtSebango3Line3.Text = sebango;
                    this.txtRemainPiece3Line3.Text = remainPiece;
                }

                if (this.lightEnbaleLine3)
                {
                    if (remainPieceNumber > 0)
                    {
                        this.lightNosWorker1Line3.Clear();
                        this.DisplayLightNo3(this.ltvLight1Line3, this.txtSebango1Line3.Text.Trim(), 1);

                        ////this.lightNosWorker2Line3.Clear();
                        ////this.DisplayLightNo3(this.ltvLight2Line3, this.txtSebango2Line3.Text.Trim(), 2);

                        ////this.lightNosWorker3Line3.Clear();
                        ////this.DisplayLightNo3(this.ltvLight3Line3, this.txtSebango3Line3.Text.Trim(), 3);

                        if (this.ltvLight1Line3.Items.Count > 0)
                        {
                            this.TurnOnLight3(this.lightNosWorker3Line3);
                        }
                        else
                        {
                            this.txtRemainPiece1Line3.Text = "0";
                        }

                        ////if (this.ltvLight2Line3.Items.Count > 0)
                        ////{
                        ////    this.TurnOnLight3(this.lightNosWorker2Line3);
                        ////}
                        ////else
                        ////{
                        ////    this.txtRemainPiece2Line3.Text = "0";
                        ////}

                        ////if (this.ltvLight3Line3.Items.Count > 0)
                        ////{
                        ////    this.TurnOnLight3(this.lightNosWorker3Line3);
                        ////}
                        ////else
                        ////{
                        ////    this.txtRemainPiece3Line3.Text = "0";
                        ////}
                    }
                }

                if (string.Equals(ConfigInfo.IsPrint3.ToUpper(), "T"))
                {
                    this.Print(this.printerNameWorker1Line3, ConfigInfo.LineName3, ConfigInfo.LineCode3,
                               sebango, remainPiece, sequence, productName, 2);

                    this.Print(this.printerNameWorker2Line3, ConfigInfo.LineName3, ConfigInfo.LineCode3,
                               sebango, remainPiece, sequence, productName, 2);

                    this.Print(this.printerNameWorker3Line3, ConfigInfo.LineName3, ConfigInfo.LineCode3,
                               sebango, remainPiece, sequence, productName, 2);
                }
            }
        }


        /// <summary>
        /// Display Light No 1
        /// </summary>
        /// <param name="ltvLight"></param>
        /// <param name="inputSebangoNo"></param>
        /// <param name="workerNumber"></param>
        private void DisplayLightNo1(ListView ltvLight, string inputSebangoNo, int workerNumber)
        {
            ltvLight.Items.Clear();

            string lightNoToTurnOn = string.Empty;
            int sebango = 0;

            if (int.TryParse(inputSebangoNo.Trim(), out sebango))
            {
                foreach (DataRow row in this.modelSebangoDNoLine1.SebangoAndDNoSetupTable.Rows)
                {
                    int sebagoDNo = 0;

                    if (int.TryParse(row["Sebango_ID"].ToString().Trim(), out sebagoDNo))
                    {
                        if (sebango == sebagoDNo)
                        {
                            string currentDNo = row["Design_No"].ToString().Trim();
                            foreach (DataRow rowLight in this.modelLightDNoLine1.LightAndDNoSetupTable.Rows)
                            {
                                if (string.Equals(currentDNo, rowLight["Design_No"].ToString().Trim()))
                                {
                                    string lightNo = rowLight["Light_No"].ToString().Trim();

                                    int lightNumber = -1;

                                    int.TryParse(lightNo, out lightNumber);

                                    int startLightNo = this.GetLightStartNo1(workerNumber);
                                    int endLightNo = this.GetLightEndNo1(workerNumber);

                                    if ((lightNumber >= startLightNo)
                                        && (lightNumber <= endLightNo))
                                    {
                                        ltvLight.Items.Add(lightNumber.ToString("0000"));

                                        if (workerNumber == 1)
                                        {
                                            this.lightNosWorker1Line1.Add(lightNumber.ToString("0000"));
                                        }
                                        else if (workerNumber == 2)
                                        {
                                            this.lightNosWorker2Line1.Add(lightNumber.ToString("0000"));
                                        }
                                        else if (workerNumber == 3)
                                        {
                                            this.lightNosWorker3Line1.Add(lightNumber.ToString("0000"));
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }


        /// <summary>
        /// Display Light No 2
        /// </summary>
        /// <param name="ltvLight"></param>
        /// <param name="inputSebangoNo"></param>
        /// <param name="workerNumber"></param>
        private void DisplayLightNo2(ListView ltvLight, string inputSebangoNo, int workerNumber)
        {
            ltvLight.Items.Clear();

            string lightNoToTurnOn = string.Empty;
            int sebango = 0;

            if (int.TryParse(inputSebangoNo.Trim(), out sebango))
            {
                foreach (DataRow row in this.modelSebangoDNoLine2.SebangoAndDNoSetupTable.Rows)
                {
                    int sebagoDNo = 0;

                    if (int.TryParse(row["Sebango_ID"].ToString().Trim(), out sebagoDNo))
                    {
                        if (sebango == sebagoDNo)
                        {
                            string currentDNo = row["Design_No"].ToString().Trim();
                            foreach (DataRow rowLight in this.modelLightDNoLine2.LightAndDNoSetupTable.Rows)
                            {
                                if (string.Equals(currentDNo, rowLight["Design_No"].ToString().Trim()))
                                {
                                    string lightNo = rowLight["Light_No"].ToString().Trim();

                                    int lightNumber = -1;

                                    int.TryParse(lightNo, out lightNumber);

                                    int startLightNo = this.GetLightStartNo2(workerNumber);
                                    int endLightNo = this.GetLightEndNo2(workerNumber);

                                    if ((lightNumber >= startLightNo)
                                        && (lightNumber <= endLightNo))
                                    {
                                        ltvLight.Items.Add(lightNumber.ToString("0000"));

                                        if (workerNumber == 1)
                                        {
                                            this.lightNosWorker1Line2.Add(lightNumber.ToString("0000"));
                                        }
                                        else if (workerNumber == 2)
                                        {
                                            this.lightNosWorker2Line2.Add(lightNumber.ToString("0000"));
                                        }
                                        else if (workerNumber == 3)
                                        {
                                            this.lightNosWorker3Line2.Add(lightNumber.ToString("0000"));
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }


        /// <summary>
        /// Display Light No 3
        /// </summary>
        /// <param name="ltvLight"></param>
        /// <param name="inputSebangoNo"></param>
        /// <param name="workerNumber"></param>
        private void DisplayLightNo3(ListView ltvLight, string inputSebangoNo, int workerNumber)
        {
            ltvLight.Items.Clear();

            string lightNoToTurnOn = string.Empty;
            int sebango = 0;

            if (int.TryParse(inputSebangoNo.Trim(), out sebango))
            {
                foreach (DataRow row in this.modelSebangoDNoLine3.SebangoAndDNoSetupTable.Rows)
                {
                    int sebagoDNo = 0;

                    if (int.TryParse(row["Sebango_ID"].ToString().Trim(), out sebagoDNo))
                    {
                        if (sebango == sebagoDNo)
                        {
                            string currentDNo = row["Design_No"].ToString().Trim();
                            foreach (DataRow rowLight in this.modelLightDNoLine3.LightAndDNoSetupTable.Rows)
                            {
                                if (string.Equals(currentDNo, rowLight["Design_No"].ToString().Trim()))
                                {
                                    string lightNo = rowLight["Light_No"].ToString().Trim();

                                    int lightNumber = -1;

                                    int.TryParse(lightNo, out lightNumber);

                                    int startLightNo = this.GetLightStartNo3(workerNumber);
                                    int endLightNo = this.GetLightEndNo3(workerNumber);

                                    if ((lightNumber >= startLightNo)
                                        && (lightNumber <= endLightNo))
                                    {
                                        ltvLight.Items.Add(lightNumber.ToString("0000"));

                                        if (workerNumber == 1)
                                        {
                                            this.lightNosWorker1Line3.Add(lightNumber.ToString("0000"));
                                        }
                                        else if (workerNumber == 2)
                                        {
                                            this.lightNosWorker2Line3.Add(lightNumber.ToString("0000"));
                                        }
                                        else if (workerNumber == 3)
                                        {
                                            this.lightNosWorker3Line3.Add(lightNumber.ToString("0000"));
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }


        /// <summary>
        /// Print
        /// </summary>
        /// <param name="printerName"></param>
        /// <param name="lineName"></param>
        /// <param name="lineCode"></param>
        /// <param name="sebango"></param>
        /// <param name="remainPiece"></param>
        /// <param name="sequence"></param>
        /// <param name="productWC"></param>
        private void Print(string printerName, string lineName, string lineCode,
                           string sebango, string remainPiece,
                           string sequence, string productWC, int line)
        {
            if (string.IsNullOrWhiteSpace(printerName))
            {
                return;
            }

            int remainPieceNumber = 0;
            int.TryParse(remainPiece, out remainPieceNumber);

            if (remainPieceNumber <= 0)
            {
                return;
            }

            Thread thread = new Thread(new ThreadStart(() =>
            {
                if (string.IsNullOrEmpty(printerName)) return;
                string Line = PickingBusiness.Configuration.GetLineCode(lineCode);
                this.modelSebangoAndProductBook = PickingBusiness.Configuration.GetSebangoAndProductBook(Line);
                string filename =  ConfigurationManager.AppSettings["FileName"].ToString().Trim();
                if (this.modelSebangoAndProductBook.SebangoAndProductBookTable.Rows.Count > 0)
                {
                    foreach (DataRow row in this.modelSebangoAndProductBook.SebangoAndProductBookTable.Rows)
                    {
                        if (string.Equals(sebango, row["Sebango_ID"].ToString().Trim()))
                        {
                            filename = row["ProductBook_Name"].ToString().Trim();
                        }
                    }
                }
                string filePath = AppDomain.CurrentDomain.BaseDirectory
                    + ConfigurationManager.AppSettings["DataFileSavePath"].ToString().Trim() + filename;

                ////string printerName = "";//set printer name
                LabelManager2.ApplicationClass application = new LabelManager2.ApplicationClass();
                LabelManager2.Document doc = application.Documents.Open(filePath, true);
                if (printerName != null)
                {
                    doc.Printer.SwitchTo(printerName);
                }
                FormVariables vars = doc.Variables.FormVariables;

                //barcode：01012341001D
                LabelManager2.Free free1 = doc.Variables.FormVariables.Item("B");
                free1.Value = lineName;    // "R1";//line name
                LabelManager2.Free free2 = doc.Variables.FormVariables.Item("WC");
                free2.Value = lineCode;    // "310";//line code
                LabelManager2.Free free3 = doc.Variables.FormVariables.Item("SEBANGO1");
                free3.Value = sebango;     // "1234";//背番号
                LabelManager2.Free free4 = doc.Variables.FormVariables.Item("ALL");
                free4.Value = remainPiece; // "10";//残留台数
                //LabelManager2.Free free5 = doc.Variables.FormVariables.Item("JUNO");
                //free5.Value = sequence;    // "01";//顺番
                LabelManager2.Free free5 = doc.Variables.FormVariables.Item("ZCNT");
                free5.Value = sequence;    // "01";//顺番
                LabelManager2.Free free6 = doc.Variables.FormVariables.Item("JIBU");
                free6.Value = productWC;   // "010";//电装品WC
                doc.PrintLabel(remainPieceNumber);
                doc.FormFeed();
                doc.Close(false);
                application.Quit();
            }));
            thread.IsBackground = true;
            thread.Start();
        }

        /// <summary>
        /// Turn On Light 1
        /// </summary>
        /// <param name="lightNos"></param>
        private void TurnOnLight1(List<string> lightNos)
        {
            string lightNoToTurnOn = string.Empty;

            foreach (string item in lightNos)
            {
                lightNoToTurnOn += item + "88888";

                if (lightNoToTurnOn.Trim().Length >= (ConfigInfo.LightNumInOneCommand * 9))
                {
                    this.axAiPlug1.SendCommand("P100" + lightNoToTurnOn.Trim());
                    lightNoToTurnOn = string.Empty;
                }
            }

            if (!string.IsNullOrEmpty(lightNoToTurnOn.Trim()))
            {
                this.axAiPlug1.SendCommand("P100" + lightNoToTurnOn.Trim());
            }
        }

        /// <summary>
        /// Turn On Light 2
        /// </summary>
        /// <param name="lightNos"></param>
        private void TurnOnLight2(List<string> lightNos)
        {
            string lightNoToTurnOn = string.Empty;

            foreach (string item in lightNos)
            {
                lightNoToTurnOn += item + "88888";

                if (lightNoToTurnOn.Trim().Length >= (ConfigInfo.LightNumInOneCommand * 9))
                {
                    this.axAiPlug2.SendCommand("P100" + lightNoToTurnOn.Trim());
                    lightNoToTurnOn = string.Empty;
                }
            }

            if (!string.IsNullOrEmpty(lightNoToTurnOn.Trim()))
            {
                this.axAiPlug2.SendCommand("P100" + lightNoToTurnOn.Trim());
            }
        }

        /// <summary>
        /// Turn On Light 3
        /// </summary>
        /// <param name="lightNos"></param>
        private void TurnOnLight3(List<string> lightNos)
        {
            string lightNoToTurnOn = string.Empty;

            foreach (string item in lightNos)
            {
                lightNoToTurnOn += item + "88888";

                if (lightNoToTurnOn.Trim().Length >= (ConfigInfo.LightNumInOneCommand * 9))
                {
                    this.axAiPlug3.SendCommand("P100" + lightNoToTurnOn.Trim());
                    lightNoToTurnOn = string.Empty;
                }
            }

            if (!string.IsNullOrEmpty(lightNoToTurnOn.Trim()))
            {
                this.axAiPlug3.SendCommand("P100" + lightNoToTurnOn.Trim());
            }
        }

        /// <summary>
        /// Turn On LED Green
        /// </summary>
        private void TurnOnLEDGreen()
        {
            this.axAiPlug1.SendCommand("T" + ConfigInfo.CmdLEDGreenLine1.Trim());
            this.axAiPlug2.SendCommand("T" + ConfigInfo.CmdLEDGreenLine2.Trim());
            this.axAiPlug3.SendCommand("T" + ConfigInfo.CmdLEDGreenLine3.Trim());
        }

        /// <summary>
        /// Turn On LED Red 1
        /// </summary>
        private void TurnOnLEDRed1()
        {
            this.axAiPlug1.SendCommand("T" + ConfigInfo.CmdLEDRedLine1.Trim());
        }

        /// <summary>
        /// Turn On LED Red 2
        /// </summary>
        private void TurnOnLEDRed2()
        {
            this.axAiPlug2.SendCommand("T" + ConfigInfo.CmdLEDRedLine2.Trim());
        }

        /// <summary>
        /// Turn On LED Red 3
        /// </summary>
        private void TurnOnLEDRed3()
        {
            this.axAiPlug3.SendCommand("T" + ConfigInfo.CmdLEDRedLine3.Trim());
        }

        /// <summary>
        /// Turn On LED Beep 1
        /// </summary>
        private void TurnOnLEDBeep1()
        {
            //this.axAiPlug1.SendCommand("T" + ConfigInfo.CmdLEDBeepLine1.Trim());
        }

        /// <summary>
        /// Turn On LED Beep 2
        /// </summary>
        private void TurnOnLEDBeep2()
        {
            //this.axAiPlug2.SendCommand("T" + ConfigInfo.CmdLEDBeepLine2.Trim());
        }

        /// <summary>
        /// Turn On LED Beep 3
        /// </summary>
        private void TurnOnLEDBeep3()
        {
            //this.axAiPlug3.SendCommand("T" + ConfigInfo.CmdLEDBeepLine3.Trim());
        }

        /// <summary>
        /// Turn On LED Red And Beep 1
        /// </summary>
        private void TurnOnLEDRedAndBeep1()
        {
            this.TurnOnLEDRed1();
            //this.TurnOnLEDBeep1();
        }

        /// <summary>
        /// Turn On LED Red And Beep 2
        /// </summary>
        private void TurnOnLEDRedAndBeep2()
        {
            this.TurnOnLEDRed2();
            //this.TurnOnLEDBeep2();
        }

        /// <summary>
        /// Turn On LED Red And Beep 3
        /// </summary>
        private void TurnOnLEDRedAndBeep3()
        {
            this.TurnOnLEDRed3();
            //this.TurnOnLEDBeep3();
        }

        /// <summary>
        /// Do Validation
        /// </summary>
        /// <param name="text"></param>
        /// <returns></returns>
        private bool DoValidation(string data, TextBox txtStatus)
        {
            string errorMessage = "扫描的序列号不正确，请重新扫描。";

            try
            {
                if (data.Length < 18)
                {
                    throw new Exception(errorMessage);
                }

                string port = this.GetScannerPort(data);
                string dataStatus = this.GetDataStatus(data);
                int barcodeLength = this.GetBarcodeLength(data);
                string sn = this.GetBarcode(data, barcodeLength);

                if (!string.Equals(dataStatus, "1"))
                {
                    throw new Exception(errorMessage);
                }

                if (barcodeLength != sn.Length)
                {
                    throw new Exception(errorMessage);
                }

                if (!this.CheckSN(sn))
                {
                    throw new Exception(errorMessage);

#if DEBUG
                    throw new Exception(errorMessage + sn + port);
#endif
                }
            }
            catch (Exception ex)
            {
                txtStatus.Text = ex.Message;

                return false;
            }

            string message = "条码读取正确";

            txtStatus.Text = message;

            return true;
        }

        /// <summary>
        /// Check SN
        /// </summary>
        /// <param name="sn"></param>
        /// <returns></returns>
        private bool CheckSN(string sn)
        {
            bool isSNValid = false;
            string checkedStr = sn.Substring(0, sn.Length - 1);
            string finalStr = checkedStr;
            int bcc = 0;
            for (int i = 0; i < checkedStr.Length; i++)
            {
                int val = 0;
                string key = checkedStr[i].ToString();
                if (this.mapping.ContainsKey(key))
                {
                    val = int.Parse(this.mapping[key]);
                }

                bcc += val;
            }

            string value = (bcc % 43).ToString();
            string concatValue = string.Empty;
            if (this.mapping.ContainsValue(value))
            {
                concatValue = this.mapping.First(p => p.Value == value).Key;
            }

            finalStr = finalStr + concatValue;
            isSNValid = (sn == finalStr);
            return isSNValid;
        }

        /// <summary>
        /// Get Light Start No 1
        /// </summary>
        /// <param name="workerNumber"></param>
        /// <returns></returns>
        private int GetLightStartNo1(int workerNumber)
        {
            int startLightNo = 0;

            foreach (DataRow row in this.modelWorkersAndLightsLine1.WorkersAndLightsSetupTable.Rows)
            {
                if (string.Equals(row["Picking_Worker_No"].ToString().Trim(), workerNumber.ToString()))
                {
                    int.TryParse(row["Light_Start_No"].ToString().Trim(), out startLightNo);
                }
            }

            return startLightNo;
        }

        /// <summary>
        /// Get Light Start No 2
        /// </summary>
        /// <param name="workerNumber"></param>
        /// <returns></returns>
        private int GetLightStartNo2(int workerNumber)
        {
            int startLightNo = 0;

            foreach (DataRow row in this.modelWorkersAndLightsLine2.WorkersAndLightsSetupTable.Rows)
            {
                if (string.Equals(row["Picking_Worker_No"].ToString().Trim(), workerNumber.ToString()))
                {
                    int.TryParse(row["Light_Start_No"].ToString().Trim(), out startLightNo);
                }
            }

            return startLightNo;
        }

        /// <summary>
        /// Get Light Start No 3
        /// </summary>
        /// <param name="workerNumber"></param>
        /// <returns></returns>
        private int GetLightStartNo3(int workerNumber)
        {
            int startLightNo = 0;

            foreach (DataRow row in this.modelWorkersAndLightsLine3.WorkersAndLightsSetupTable.Rows)
            {
                if (string.Equals(row["Picking_Worker_No"].ToString().Trim(), workerNumber.ToString()))
                {
                    int.TryParse(row["Light_Start_No"].ToString().Trim(), out startLightNo);
                }
            }

            return startLightNo;
        }

        /// <summary>
        /// Get Light End No 1
        /// </summary>
        /// <param name="workerNumber"></param>
        /// <returns></returns>
        private int GetLightEndNo1(int workerNumber)
        {
            int endLightNo = 0;

            foreach (DataRow row in this.modelWorkersAndLightsLine1.WorkersAndLightsSetupTable.Rows)
            {
                if (string.Equals(row["Picking_Worker_No"].ToString().Trim(), workerNumber.ToString()))
                {
                    int.TryParse(row["Light_End_No"].ToString().Trim(), out endLightNo);
                }
            }

            return endLightNo;
        }

        /// <summary>
        /// Get Light End No 2
        /// </summary>
        /// <param name="workerNumber"></param>
        /// <returns></returns>
        private int GetLightEndNo2(int workerNumber)
        {
            int endLightNo = 0;

            foreach (DataRow row in this.modelWorkersAndLightsLine2.WorkersAndLightsSetupTable.Rows)
            {
                if (string.Equals(row["Picking_Worker_No"].ToString().Trim(), workerNumber.ToString()))
                {
                    int.TryParse(row["Light_End_No"].ToString().Trim(), out endLightNo);
                }
            }

            return endLightNo;
        }

        /// <summary>
        /// Get Light End No 3
        /// </summary>
        /// <param name="workerNumber"></param>
        /// <returns></returns>
        private int GetLightEndNo3(int workerNumber)
        {
            int endLightNo = 0;

            foreach (DataRow row in this.modelWorkersAndLightsLine3.WorkersAndLightsSetupTable.Rows)
            {
                if (string.Equals(row["Picking_Worker_No"].ToString().Trim(), workerNumber.ToString()))
                {
                    int.TryParse(row["Light_End_No"].ToString().Trim(), out endLightNo);
                }
            }

            return endLightNo;
        }

        /// <summary>
        /// Get Product Name
        /// </summary>
        /// <param name="text"></param>
        /// <returns></returns>
        private string GetProductName(string text)
        {
            string productName = string.Empty;
            productName = text.Substring(0, 3);

            return productName;
        }

        /// <summary>
        /// Get Sebango
        /// </summary>
        /// <param name="text"></param>
        /// <returns></returns>
        private string GetSebango(string text)
        {
            string sebango = string.Empty;
            sebango = text.Substring(3, 4);

            return sebango;
        }

        /// <summary>
        /// Get Remain Piece
        /// </summary>
        /// <param name="text"></param>
        /// <returns></returns>
        private string GetRemainPiece(string text)
        {
            string remainPiece = string.Empty;
            remainPiece = text.Substring(7, 2);
            int intV;
            if (!int.TryParse(remainPiece, out intV)) remainPiece = "01";

            return remainPiece;
        }

        /// <summary>
        /// Get Sequence
        /// </summary>
        /// <param name="text"></param>
        /// <returns></returns>
        private string GetSequence(string text)
        {
            string sequence = string.Empty;
            //sequence = text.Substring(9, 2);

            return sequence;
        }

        /// <summary>
        /// Get Scanner Port
        /// </summary>
        /// <param name="data"></param>
        /// <returns></returns>
        private string GetScannerPort(string data)
        {
            string scannerPort = string.Empty;
            scannerPort = data.Substring(1, 4);

            return scannerPort;
        }

        /// <summary>
        /// Get Data Status
        /// </summary>
        /// <param name="data"></param>
        /// <returns></returns>
        private string GetDataStatus(string data)
        {
            string status = string.Empty;
            status = data.Substring(5, 1);

            return status;
        }

        /// <summary>
        /// Get Barcode Length
        /// </summary>
        /// <param name="data"></param>
        /// <returns></returns>
        private int GetBarcodeLength(string data)
        {
            string barcodeLength = string.Empty;
            barcodeLength = data.Substring(6, 2);

            return int.Parse(barcodeLength);
        }

        /// <summary>
        /// Get Barcode
        /// </summary>
        /// <param name="data"></param>
        /// <param name="length"></param>
        /// <returns></returns>
        private string GetBarcode(string data, int length)
        {
            string barcode = string.Empty;
            barcode = data.Substring(8, length);

            return barcode;
        }

        /// <summary>
        /// Get Light No
        /// </summary>
        /// <param name="data"></param>
        /// <returns></returns>
        private string GetLightNo(string data)
        {
            string lightNo = string.Empty;
            lightNo = data.Substring(1, 4);

            return lightNo;
        }

        /// <summary>
        /// Get Light Status
        /// </summary>
        /// <param name="data"></param>
        /// <returns></returns>
        private string GetLightStatus(string data)
        {
            string status = string.Empty;
            if (data.Length >= 7)
            {
                status = data.Substring(5, 2);
            }
            else if (data.Length >= 6)
            {
                status = data.Substring(5, data.Length - 5);
            }

            return status;
        }

        /// <summary>
        /// Handle Turned Off Light No 1
        /// </summary>
        /// <param name="data"></param>
        /// <param name="port"></param>
        private void HandleTurnedOffLightNo1(string data, string port)
        {
            if (this.currentWorkerCountLine1 <= 0)
            {
                return;
            }

            string lightNo = this.GetLightNo(data);
            string lightStatus = this.GetLightStatus(data);

            if (this.currentWorkerCountLine1 <= 1)
            {
                this.ClearLightNo1(this.ltvLight1Line1, lightNo, lightStatus, this.txtRemainPiece1Line1, this.lightNosWorker1Line1, 0);
            }
            else if (this.currentWorkerCountLine1 <= 2)
            {
                this.ClearLightNo1(this.ltvLight1Line1, lightNo, lightStatus, this.txtRemainPiece1Line1, this.lightNosWorker1Line1, 0);
                this.ClearLightNo1(this.ltvLight2Line1, lightNo, lightStatus, this.txtRemainPiece2Line1, this.lightNosWorker2Line1, 1);
            }
            else
            {
                this.ClearLightNo1(this.ltvLight1Line1, lightNo, lightStatus, this.txtRemainPiece1Line1, this.lightNosWorker1Line1, 0);
                this.ClearLightNo1(this.ltvLight2Line1, lightNo, lightStatus, this.txtRemainPiece2Line1, this.lightNosWorker2Line1, 1);
                this.ClearLightNo1(this.ltvLight3Line1, lightNo, lightStatus, this.txtRemainPiece3Line1, this.lightNosWorker3Line1, 2);
            }
        }

        /// <summary>
        /// Handle Turned Off Light No 2
        /// </summary>
        /// <param name="data"></param>
        /// <param name="port"></param>
        private void HandleTurnedOffLightNo2(string data, string port)
        {
            if (this.currentWorkerCountLine2 <= 0)
            {
                return;
            }

            string lightNo = this.GetLightNo(data);
            string lightStatus = this.GetLightStatus(data);

            if (this.currentWorkerCountLine2 <= 1)
            {
                this.ClearLightNo2(this.ltvLight1Line2, lightNo, lightStatus, this.txtRemainPiece1Line2, this.lightNosWorker1Line2, 0);
            }
            else if (this.currentWorkerCountLine2 <= 2)
            {
                this.ClearLightNo2(this.ltvLight1Line2, lightNo, lightStatus, this.txtRemainPiece1Line2, this.lightNosWorker1Line2, 0);
                this.ClearLightNo2(this.ltvLight2Line2, lightNo, lightStatus, this.txtRemainPiece2Line2, this.lightNosWorker2Line2, 1);
            }
            else
            {
                this.ClearLightNo2(this.ltvLight1Line2, lightNo, lightStatus, this.txtRemainPiece1Line2, this.lightNosWorker1Line2, 0);
                this.ClearLightNo2(this.ltvLight2Line2, lightNo, lightStatus, this.txtRemainPiece2Line2, this.lightNosWorker2Line2, 1);
                this.ClearLightNo2(this.ltvLight3Line2, lightNo, lightStatus, this.txtRemainPiece3Line2, this.lightNosWorker3Line2, 2);
            }
        }

        /// <summary>
        /// Handle Turned Off Light No 3
        /// </summary>
        /// <param name="data"></param>
        /// <param name="port"></param>
        private void HandleTurnedOffLightNo3(string data, string port)
        {
            if (this.currentWorkerCountLine3 <= 0)
            {
                return;
            }

            string lightNo = this.GetLightNo(data);
            string lightStatus = this.GetLightStatus(data);

            if (this.currentWorkerCountLine3 <= 1)
            {
                this.ClearLightNo3(this.ltvLight1Line3, lightNo, lightStatus, this.txtRemainPiece1Line3, this.lightNosWorker1Line3, 0);
            }
            else if (this.currentWorkerCountLine3 <= 2)
            {
                this.ClearLightNo3(this.ltvLight1Line3, lightNo, lightStatus, this.txtRemainPiece1Line3, this.lightNosWorker1Line3, 0);
                this.ClearLightNo3(this.ltvLight2Line3, lightNo, lightStatus, this.txtRemainPiece2Line3, this.lightNosWorker2Line3, 1);
            }
            else
            {
                this.ClearLightNo3(this.ltvLight1Line3, lightNo, lightStatus, this.txtRemainPiece1Line3, this.lightNosWorker1Line3, 0);
                this.ClearLightNo3(this.ltvLight2Line3, lightNo, lightStatus, this.txtRemainPiece2Line3, this.lightNosWorker2Line3, 1);
                this.ClearLightNo3(this.ltvLight3Line3, lightNo, lightStatus, this.txtRemainPiece3Line3, this.lightNosWorker3Line3, 2);
            }
        }


        /// <summary>
        /// Clear Light No 1
        /// </summary>
        /// <param name="ltvLight"></param>
        /// <param name="lightNo"></param>
        /// <param name="lightStatus"></param>
        private void ClearLightNo1(ListView ltvLight, string lightNo, string lightStatus,
                                  TextBox txtRemainPiece, List<string> lightNos, int lineNum)
        {
            bool findLight = false;

            for (int i = ltvLight.Items.Count - 1; i >= 0; i--)
            {
                if (string.Equals(ltvLight.Items[i].Text.Substring(0, lightNo.Length), lightNo))
                {
                    findLight = true;

                    if (string.Equals(lightStatus, "00"))
                    {
                        ////ltvLight.Items[i].Remove();
                        ////ltvLight.Items[i].Text = lightNo;
                        ltvLight.Items[i].ForeColor = System.Drawing.Color.Gray;
                    }
                    else
                    {
                        ////ltvLight.Items[i].Text = lightNo;
                        ltvLight.Items[i].ForeColor = System.Drawing.Color.Red;
                        this.TurnOnLEDRedAndBeep1();
                    }
                }
            }

            ////if (findLight && (ltvLight.Items.Count == 0))
            if (findLight && this.IsAllLightTurnedOff(ltvLight))
            {
                ltvLight.Items.Clear();

                int remainPiece = 0;

                int.TryParse(txtRemainPiece.Text.Trim(), out remainPiece);

                remainPiece--;

                if (lineNum == 0) TurnOnBeep0(this.axAiPlug1);
                if (lineNum == 1) TurnOnBeep1(this.axAiPlug1);
                if (lineNum == 2) TurnOnBeep2(this.axAiPlug1);

                if (remainPiece >= 0)
                {
                    txtRemainPiece.Text = remainPiece.ToString();

                    if (remainPiece > 0)
                    {
                        bool isTurnOnLight = true;

                        if (lineNum == 2)
                        {
                            int remainPiece3 = 0;
                            int.TryParse(this.txtRemainPiece3Line1.Text.Trim(), out remainPiece3);

                            int remainPiece2 = 0;
                            int.TryParse(this.txtRemainPiece2Line1.Text.Trim(), out remainPiece2);

                            if (remainPiece3 <= remainPiece2)
                            {
                                isTurnOnLight = false;
                            }
                        }

                        if (lineNum == 1)
                        {
                            int remainPiece2 = 0;
                            int.TryParse(this.txtRemainPiece2Line1.Text.Trim(), out remainPiece2);

                            int remainPiece1 = 0;
                            int.TryParse(this.txtRemainPiece1Line1.Text.Trim(), out remainPiece1);

                            if (remainPiece2 <= remainPiece1)
                            {
                                isTurnOnLight = false;
                            }
                        }

                        if (isTurnOnLight)
                        {
                            foreach (string item in lightNos)
                            {
                                ltvLight.Items.Add(item);
                            }

                            this.TurnOnLight1(lightNos);
                        }
                    }
                    else
                    {
                        lightNos.Clear();
                        //如果当前流水线的棚灯已经全部熄灭，那么将该流水线对应的队列的序列号出列
                        QueueListLine1[lineNum].Dequeue();
                    }
                }

                //如果当前流水线的棚灯已经全部熄灭，那么尝试将下一条流水线的棚灯打开
                TurnOnLineLightsIfPossible1();
            }
        }


        /// <summary>
        /// Clear Light No 2
        /// </summary>
        /// <param name="ltvLight"></param>
        /// <param name="lightNo"></param>
        /// <param name="lightStatus"></param>
        private void ClearLightNo2(ListView ltvLight, string lightNo, string lightStatus,
                                  TextBox txtRemainPiece, List<string> lightNos, int lineNum)
        {
            bool findLight = false;

            for (int i = ltvLight.Items.Count - 1; i >= 0; i--)
            {
                if (string.Equals(ltvLight.Items[i].Text.Substring(0, lightNo.Length), lightNo))
                {
                    findLight = true;

                    if (string.Equals(lightStatus, "00"))
                    {
                        ////ltvLight.Items[i].Remove();
                        ////ltvLight.Items[i].Text = lightNo;
                        ltvLight.Items[i].ForeColor = System.Drawing.Color.Gray;
                    }
                    else
                    {
                        ////ltvLight.Items[i].Text = lightNo;
                        ltvLight.Items[i].ForeColor = System.Drawing.Color.Red;
                        this.TurnOnLEDRedAndBeep2();
                    }
                }
            }

            ////if (findLight && (ltvLight.Items.Count == 0))
            if (findLight && this.IsAllLightTurnedOff(ltvLight))
            {
                ltvLight.Items.Clear();

                int remainPiece = 0;

                int.TryParse(txtRemainPiece.Text.Trim(), out remainPiece);

                remainPiece--;
                if (lineNum == 0) TurnOnBeep0(this.axAiPlug2);
                if (lineNum == 1) TurnOnBeep1(this.axAiPlug2);
                if (lineNum == 2) TurnOnBeep2(this.axAiPlug2);

                if (remainPiece >= 0)
                {
                    txtRemainPiece.Text = remainPiece.ToString();

                    if (remainPiece > 0)
                    {
                        bool isTurnOnLight = true;

                        if (lineNum == 2)
                        {
                            int remainPiece3 = 0;
                            int.TryParse(this.txtRemainPiece3Line2.Text.Trim(), out remainPiece3);

                            int remainPiece2 = 0;
                            int.TryParse(this.txtRemainPiece2Line2.Text.Trim(), out remainPiece2);

                            if (remainPiece3 <= remainPiece2)
                            {
                                isTurnOnLight = false;
                            }
                        }

                        if (lineNum == 1)
                        {
                            int remainPiece2 = 0;
                            int.TryParse(this.txtRemainPiece2Line2.Text.Trim(), out remainPiece2);

                            int remainPiece1 = 0;
                            int.TryParse(this.txtRemainPiece1Line2.Text.Trim(), out remainPiece1);

                            if (remainPiece2 <= remainPiece1)
                            {
                                isTurnOnLight = false;
                            }
                        }

                        if (isTurnOnLight)
                        {
                            foreach (string item in lightNos)
                            {
                                ltvLight.Items.Add(item);
                            }

                            this.TurnOnLight2(lightNos);
                        }
                    }
                    else
                    {
                        lightNos.Clear();
                        //如果当前流水线的棚灯已经全部熄灭，那么将该流水线对应的队列的序列号出列
                        QueueListLine2[lineNum].Dequeue();
                    }
                }

                //如果当前流水线的棚灯已经全部熄灭，那么尝试将下一条流水线的棚灯打开
                TurnOnLineLightsIfPossible2();
            }
        }


        /// <summary>
        /// Clear Light No 3
        /// </summary>
        /// <param name="ltvLight"></param>
        /// <param name="lightNo"></param>
        /// <param name="lightStatus"></param>
        private void ClearLightNo3(ListView ltvLight, string lightNo, string lightStatus,
                                  TextBox txtRemainPiece, List<string> lightNos, int lineNum)
        {
            bool findLight = false;

            for (int i = ltvLight.Items.Count - 1; i >= 0; i--)
            {
                if (string.Equals(ltvLight.Items[i].Text.Substring(0, lightNo.Length), lightNo))
                {
                    findLight = true;

                    if (string.Equals(lightStatus, "00"))
                    {
                        ////ltvLight.Items[i].Remove();
                        ////ltvLight.Items[i].Text = lightNo;
                        ltvLight.Items[i].ForeColor = System.Drawing.Color.Gray;
                    }
                    else
                    {
                        ////ltvLight.Items[i].Text = lightNo;
                        ltvLight.Items[i].ForeColor = System.Drawing.Color.Red;
                        this.TurnOnLEDRedAndBeep3();
                    }
                }
            }

            ////if (findLight && (ltvLight.Items.Count == 0))
            if (findLight && this.IsAllLightTurnedOff(ltvLight))
            {
                ltvLight.Items.Clear();

                int remainPiece = 0;

                int.TryParse(txtRemainPiece.Text.Trim(), out remainPiece);

                remainPiece--;
                if (lineNum == 0) TurnOnBeep0(this.axAiPlug3);
                if (lineNum == 1) TurnOnBeep1(this.axAiPlug3);
                if (lineNum == 2) TurnOnBeep2(this.axAiPlug3);

                if (remainPiece >= 0)
                {
                    txtRemainPiece.Text = remainPiece.ToString();

                    if (remainPiece > 0)
                    {
                        bool isTurnOnLight = true;

                        if (lineNum == 2)
                        {
                            int remainPiece3 = 0;
                            int.TryParse(this.txtRemainPiece3Line3.Text.Trim(), out remainPiece3);

                            int remainPiece2 = 0;
                            int.TryParse(this.txtRemainPiece2Line3.Text.Trim(), out remainPiece2);

                            if (remainPiece3 <= remainPiece2)
                            {
                                isTurnOnLight = false;
                            }
                        }

                        if (lineNum == 1)
                        {
                            int remainPiece2 = 0;
                            int.TryParse(this.txtRemainPiece2Line3.Text.Trim(), out remainPiece2);

                            int remainPiece1 = 0;
                            int.TryParse(this.txtRemainPiece1Line3.Text.Trim(), out remainPiece1);

                            if (remainPiece2 <= remainPiece1)
                            {
                                isTurnOnLight = false;
                            }
                        }

                        if (isTurnOnLight)
                        {
                            foreach (string item in lightNos)
                            {
                                ltvLight.Items.Add(item);
                            }

                            this.TurnOnLight3(lightNos);
                        }
                    }
                    else
                    {
                        lightNos.Clear();
                        //如果当前流水线的棚灯已经全部熄灭，那么将该流水线对应的队列的序列号出列
                        QueueListLine3[lineNum].Dequeue();
                    }
                }

                //如果当前流水线的棚灯已经全部熄灭，那么尝试将下一条流水线的棚灯打开
                TurnOnLineLightsIfPossible3();
            }
        }

        private Thread thread = null;
        private void TurnOnBeep2(AxAiPlugControl.AxAiPlug plugin)
        {
            plugin.SendCommand("T" + ConfigInfo.CmdLEDBeep2Line1.Trim());
            thread = new Thread(p =>
            {
                object[] obj = p as object[];
                Thread.Sleep(int.Parse(obj[2].ToString()));
                AxAiPlugControl.AxAiPlug plugin2 = obj[0] as AxAiPlugControl.AxAiPlug;
                string cmd = obj[1].ToString();
                plugin.SendCommand(cmd);
            }, 0);
            thread.Start(new object[] { plugin, "T80050", ConfigurationManager.AppSettings["BeepTime"] });
        }

        private void TurnOnBeep1(AxAiPlugControl.AxAiPlug plugin)
        {
            plugin.SendCommand("T" + ConfigInfo.CmdLEDBeep1Line1.Trim());
            thread = new Thread(p =>
            {
                object[] obj = p as object[];
                Thread.Sleep(int.Parse(obj[2].ToString()));
                AxAiPlugControl.AxAiPlug plugin2 = obj[0] as AxAiPlugControl.AxAiPlug;
                string cmd = obj[1].ToString();
                plugin.SendCommand(cmd);
            }, 0);
            thread.Start(new object[] { plugin, "T80040", ConfigurationManager.AppSettings["BeepTime"] });
        }

        private void TurnOnBeep0(AxAiPlugControl.AxAiPlug plugin)
        {
            plugin.SendCommand("T" + ConfigInfo.CmdLEDBeep0Line1.Trim());
            thread = new Thread(p =>
            {
                object[] obj = p as object[];
                Thread.Sleep(int.Parse(obj[2].ToString()));
                AxAiPlugControl.AxAiPlug plugin2 = obj[0] as AxAiPlugControl.AxAiPlug;
                string cmd = obj[1].ToString();
                plugin.SendCommand(cmd);
            }, 0);
            thread.Start(new object[] { plugin, "T80020", ConfigurationManager.AppSettings["BeepTime"] });
        }

        private bool IsRemainPieceZero(string remainPiece)
        {
            bool isRemainPieceZero = false;

            if (string.IsNullOrWhiteSpace(remainPiece.Trim())
                || "0".Equals(remainPiece.Trim()))
            {
                isRemainPieceZero = true;
            }

            return isRemainPieceZero;
        }

        private void TurnOnLineLightsIfPossible1()
        {
            bool checkRemaincePieceForTurnOnLight = false;
            bool isTurnOnLight = true;

            for (int i = this.currentWorkerCountLine1 - 1; i >= 1; i--)
            {
                checkRemaincePieceForTurnOnLight = false;
                isTurnOnLight = true;

                Queue<string> currentCheckQueue = QueueListLine1[i];
                Queue<string> prevCheckQueue = QueueListLine1[i - 1];

                //当前队列的序列号数目比前一个队列的序列号数目多并且当前队列对应的流水线没有灯在打开状态下那么获取当前队列的
                //最前面一个序列号，并将该序列号对应的棚灯打开并显示，否则返回
                if (currentCheckQueue.Count < prevCheckQueue.Count)
                {
                    continue;
                }

                if (currentCheckQueue.Count == prevCheckQueue.Count)
                {
                    if (i == 2)
                    {
                        int remainPiece3 = 0;
                        int.TryParse(this.txtRemainPiece3Line1.Text.Trim(), out remainPiece3);

                        int remainPiece2 = 0;
                        int.TryParse(this.txtRemainPiece2Line1.Text.Trim(), out remainPiece2);

                        if (remainPiece3 <= remainPiece2)
                        {
                            if (remainPiece3 > 0)
                            {
                                continue;
                            }
                            else
                            {
                                checkRemaincePieceForTurnOnLight = true;
                            }
                        }
                    }

                    if (i == 1)
                    {
                        int remainPiece2 = 0;
                        int.TryParse(this.txtRemainPiece2Line1.Text.Trim(), out remainPiece2);

                        int remainPiece1 = 0;
                        int.TryParse(this.txtRemainPiece1Line1.Text.Trim(), out remainPiece1);

                        if ((remainPiece2 <= remainPiece1))
                        {
                            if (remainPiece2 > 0)
                            {
                                continue;
                            }
                            else
                            {
                                checkRemaincePieceForTurnOnLight = true;
                            }
                        }
                    }
                }

                if (IsCheckedQueueAlreadyHasLighsOn1(i + 1))
                {
                    continue;
                }

                if (currentCheckQueue.Count > 0)
                {
                    string sn = currentCheckQueue.Peek();

                    string productName = this.GetProductName(sn.Trim());
                    string sebango = this.GetSebango(sn.Trim());
                    string remainPiece = this.GetRemainPiece(sn.Trim());
                    string sequence = this.GetSequence(sn.Trim());

                    if (i == 2)
                    {
                        this.txtProduct3Line1.Text = productName;
                        this.txtSebango3Line1.Text = sebango;

                        if (this.IsRemainPieceZero(this.txtRemainPiece3Line1.Text.Trim()))
                        {
                            this.txtRemainPiece3Line1.Text = remainPiece;
                        }

                        if (checkRemaincePieceForTurnOnLight)
                        {
                            int remainPiece3 = 0;
                            int.TryParse(this.txtRemainPiece3Line1.Text.Trim(), out remainPiece3);

                            int remainPiece2 = 0;
                            int.TryParse(this.txtRemainPiece2Line1.Text.Trim(), out remainPiece2);

                            if ((remainPiece3 <= remainPiece2))
                            {
                                isTurnOnLight = false;
                            }
                        }

                        if (isTurnOnLight)
                        {
                            this.lightNosWorker3Line1.Clear();
                            this.DisplayLightNo1(this.ltvLight3Line1, this.txtSebango3Line1.Text.Trim(), 3);
                            if (this.ltvLight3Line1.Items.Count > 0) this.TurnOnLight1(this.lightNosWorker3Line1);
                        }
                    }
                    else if (i == 1)
                    {
                        this.txtProduct2Line1.Text = productName;
                        this.txtSebango2Line1.Text = sebango;

                        if (this.IsRemainPieceZero(this.txtRemainPiece2Line1.Text.Trim()))
                        {
                            this.txtRemainPiece2Line1.Text = remainPiece;
                        }

                        if (checkRemaincePieceForTurnOnLight)
                        {
                            int remainPiece2 = 0;
                            int.TryParse(this.txtRemainPiece2Line1.Text.Trim(), out remainPiece2);

                            int remainPiece1 = 0;
                            int.TryParse(this.txtRemainPiece1Line1.Text.Trim(), out remainPiece1);

                            if ((remainPiece2 <= remainPiece1))
                            {
                                isTurnOnLight = false;
                            }
                        }

                        if (isTurnOnLight)
                        {
                            this.lightNosWorker2Line1.Clear();
                            this.DisplayLightNo1(this.ltvLight2Line1, this.txtSebango2Line1.Text.Trim(), 2);
                            if (this.ltvLight2Line1.Items.Count > 0) this.TurnOnLight1(this.lightNosWorker2Line1);
                        }
                    }
                }
            }
        }

        private void TurnOnLineLightsIfPossible2()
        {
            bool checkRemaincePieceForTurnOnLight = false;
            bool isTurnOnLight = true;

            for (int i = this.currentWorkerCountLine2 - 1; i >= 1; i--)
            {
                checkRemaincePieceForTurnOnLight = false;
                isTurnOnLight = true;

                Queue<string> currentCheckQueue = QueueListLine2[i];
                Queue<string> prevCheckQueue = QueueListLine2[i - 1];

                //当前队列的序列号数目比前一个队列的序列号数目多并且当前队列对应的流水线没有灯在打开状态下那么获取当前队列的
                //最前面一个序列号，并将该序列号对应的棚灯打开并显示，否则返回
                if (currentCheckQueue.Count < prevCheckQueue.Count)
                {
                    continue;
                }

                if (currentCheckQueue.Count == prevCheckQueue.Count)
                {
                    if (i == 2)
                    {
                        int remainPiece3 = 0;
                        int.TryParse(this.txtRemainPiece3Line2.Text.Trim(), out remainPiece3);

                        int remainPiece2 = 0;
                        int.TryParse(this.txtRemainPiece2Line2.Text.Trim(), out remainPiece2);

                        if (remainPiece3 <= remainPiece2)
                        {
                            if (remainPiece3 > 0)
                            {
                                continue;
                            }
                            else
                            {
                                checkRemaincePieceForTurnOnLight = true;
                            }
                        }
                    }

                    if (i == 1)
                    {
                        int remainPiece2 = 0;
                        int.TryParse(this.txtRemainPiece2Line2.Text.Trim(), out remainPiece2);

                        int remainPiece1 = 0;
                        int.TryParse(this.txtRemainPiece1Line2.Text.Trim(), out remainPiece1);

                        if ((remainPiece2 <= remainPiece1))
                        {
                            if (remainPiece2 > 0)
                            {
                                continue;
                            }
                            else
                            {
                                checkRemaincePieceForTurnOnLight = true;
                            }
                        }
                    }
                }

                if (IsCheckedQueueAlreadyHasLighsOn2(i + 1))
                {
                    continue;
                }

                if (currentCheckQueue.Count > 0)
                {
                    string sn = currentCheckQueue.Peek();

                    string productName = this.GetProductName(sn.Trim());
                    string sebango = this.GetSebango(sn.Trim());
                    string remainPiece = this.GetRemainPiece(sn.Trim());
                    string sequence = this.GetSequence(sn.Trim());

                    if (i == 2)
                    {
                        this.txtProduct3Line2.Text = productName;
                        this.txtSebango3Line2.Text = sebango;

                        if (this.IsRemainPieceZero(this.txtRemainPiece3Line2.Text.Trim()))
                        {
                            this.txtRemainPiece3Line2.Text = remainPiece;
                        }

                        if (checkRemaincePieceForTurnOnLight)
                        {
                            int remainPiece3 = 0;
                            int.TryParse(this.txtRemainPiece3Line2.Text.Trim(), out remainPiece3);

                            int remainPiece2 = 0;
                            int.TryParse(this.txtRemainPiece2Line2.Text.Trim(), out remainPiece2);

                            if ((remainPiece3 <= remainPiece2))
                            {
                                isTurnOnLight = false;
                            }
                        }

                        if (isTurnOnLight)
                        {
                            this.lightNosWorker3Line2.Clear();
                            this.DisplayLightNo2(this.ltvLight3Line2, this.txtSebango3Line2.Text.Trim(), 3);
                            if (this.ltvLight3Line2.Items.Count > 0) this.TurnOnLight2(this.lightNosWorker3Line2);
                        }
                    }
                    else if (i == 1)
                    {
                        this.txtProduct2Line2.Text = productName;
                        this.txtSebango2Line2.Text = sebango;

                        if (this.IsRemainPieceZero(this.txtRemainPiece2Line2.Text.Trim()))
                        {
                            this.txtRemainPiece2Line2.Text = remainPiece;
                        }

                        if (checkRemaincePieceForTurnOnLight)
                        {
                            int remainPiece2 = 0;
                            int.TryParse(this.txtRemainPiece2Line2.Text.Trim(), out remainPiece2);

                            int remainPiece1 = 0;
                            int.TryParse(this.txtRemainPiece1Line2.Text.Trim(), out remainPiece1);

                            if ((remainPiece2 <= remainPiece1))
                            {
                                isTurnOnLight = false;
                            }
                        }

                        if (isTurnOnLight)
                        {
                            this.lightNosWorker2Line2.Clear();
                            this.DisplayLightNo2(this.ltvLight2Line2, this.txtSebango2Line2.Text.Trim(), 2);
                            if (this.ltvLight2Line2.Items.Count > 0) this.TurnOnLight2(this.lightNosWorker2Line2);
                        }
                    }
                }
            }
        }

        private void TurnOnLineLightsIfPossible3()
        {
            bool checkRemaincePieceForTurnOnLight = false;
            bool isTurnOnLight = true;

            for (int i = this.currentWorkerCountLine3 - 1; i >= 1; i--)
            {
                checkRemaincePieceForTurnOnLight = false;
                isTurnOnLight = true;

                Queue<string> currentCheckQueue = QueueListLine3[i];
                Queue<string> prevCheckQueue = QueueListLine3[i - 1];

                //当前队列的序列号数目比前一个队列的序列号数目多并且当前队列对应的流水线没有灯在打开状态下那么获取当前队列的
                //最前面一个序列号，并将该序列号对应的棚灯打开并显示，否则返回
                if (currentCheckQueue.Count < prevCheckQueue.Count)
                {
                    continue;
                }

                if (currentCheckQueue.Count == prevCheckQueue.Count)
                {
                    if (i == 2)
                    {
                        int remainPiece3 = 0;
                        int.TryParse(this.txtRemainPiece3Line3.Text.Trim(), out remainPiece3);

                        int remainPiece2 = 0;
                        int.TryParse(this.txtRemainPiece2Line3.Text.Trim(), out remainPiece2);

                        if (remainPiece3 <= remainPiece2)
                        {
                            if (remainPiece3 > 0)
                            {
                                continue;
                            }
                            else
                            {
                                checkRemaincePieceForTurnOnLight = true;
                            }
                        }
                    }

                    if (i == 1)
                    {
                        int remainPiece2 = 0;
                        int.TryParse(this.txtRemainPiece2Line3.Text.Trim(), out remainPiece2);

                        int remainPiece1 = 0;
                        int.TryParse(this.txtRemainPiece1Line3.Text.Trim(), out remainPiece1);

                        if ((remainPiece2 <= remainPiece1))
                        {
                            if (remainPiece2 > 0)
                            {
                                continue;
                            }
                            else
                            {
                                checkRemaincePieceForTurnOnLight = true;
                            }
                        }
                    }
                }

                if (IsCheckedQueueAlreadyHasLighsOn3(i + 1))
                {
                    continue;
                }

                if (currentCheckQueue.Count > 0)
                {
                    string sn = currentCheckQueue.Peek();

                    string productName = this.GetProductName(sn.Trim());
                    string sebango = this.GetSebango(sn.Trim());
                    string remainPiece = this.GetRemainPiece(sn.Trim());
                    string sequence = this.GetSequence(sn.Trim());

                    if (i == 2)
                    {
                        this.txtProduct3Line3.Text = productName;
                        this.txtSebango3Line3.Text = sebango;

                        if (this.IsRemainPieceZero(this.txtRemainPiece3Line3.Text.Trim()))
                        {
                            this.txtRemainPiece3Line3.Text = remainPiece;
                        }

                        if (checkRemaincePieceForTurnOnLight)
                        {
                            int remainPiece3 = 0;
                            int.TryParse(this.txtRemainPiece3Line3.Text.Trim(), out remainPiece3);

                            int remainPiece2 = 0;
                            int.TryParse(this.txtRemainPiece2Line3.Text.Trim(), out remainPiece2);

                            if ((remainPiece3 <= remainPiece2))
                            {
                                isTurnOnLight = false;
                            }
                        }

                        if (isTurnOnLight)
                        {
                            this.lightNosWorker3Line3.Clear();
                            this.DisplayLightNo3(this.ltvLight3Line3, this.txtSebango3Line3.Text.Trim(), 3);
                            if (this.ltvLight3Line3.Items.Count > 0) this.TurnOnLight3(this.lightNosWorker3Line3);
                        }
                    }
                    else if (i == 1)
                    {
                        this.txtProduct2Line3.Text = productName;
                        this.txtSebango2Line3.Text = sebango;

                        if (this.IsRemainPieceZero(this.txtRemainPiece2Line3.Text.Trim()))
                        {
                            this.txtRemainPiece2Line3.Text = remainPiece;
                        }

                        if (checkRemaincePieceForTurnOnLight)
                        {
                            int remainPiece2 = 0;
                            int.TryParse(this.txtRemainPiece2Line3.Text.Trim(), out remainPiece2);

                            int remainPiece1 = 0;
                            int.TryParse(this.txtRemainPiece1Line3.Text.Trim(), out remainPiece1);

                            if ((remainPiece2 <= remainPiece1))
                            {
                                isTurnOnLight = false;
                            }
                        }

                        if (isTurnOnLight)
                        {
                            this.lightNosWorker2Line3.Clear();
                            this.DisplayLightNo3(this.ltvLight2Line3, this.txtSebango2Line3.Text.Trim(), 2);
                            if (this.ltvLight2Line3.Items.Count > 0) this.TurnOnLight3(this.lightNosWorker2Line3);
                        }
                    }
                }
            }
        }

        private bool IsCheckedQueueAlreadyHasLighsOn1(int checkedLine)
        {
            if (checkedLine == 2)
            {
                if (this.ltvLight2Line1.Items.Count > 0) return true;
                else return false;
            }
            else if (checkedLine == 3)
            {
                if (this.ltvLight3Line1.Items.Count > 0) return true;
                else return false;
            }
            return true;
        }

        private bool IsCheckedQueueAlreadyHasLighsOn2(int checkedLine)
        {
            if (checkedLine == 2)
            {
                if (this.ltvLight2Line2.Items.Count > 0) return true;
                else return false;
            }
            else if (checkedLine == 3)
            {
                if (this.ltvLight3Line2.Items.Count > 0) return true;
                else return false;
            }
            return true;
        }

        private bool IsCheckedQueueAlreadyHasLighsOn3(int checkedLine)
        {
            if (checkedLine == 2)
            {
                if (this.ltvLight2Line3.Items.Count > 0) return true;
                else return false;
            }
            else if (checkedLine == 3)
            {
                if (this.ltvLight3Line3.Items.Count > 0) return true;
                else return false;
            }
            return true;
        }

        /// <summary>
        /// Is All Light Turned Off
        /// </summary>
        /// <param name="ltvLight"></param>
        /// <returns></returns>
        private bool IsAllLightTurnedOff(ListView ltvLight)
        {
            bool isAllLightTurnedOff = true;

            for (int i = ltvLight.Items.Count - 1; i >= 0; i--)
            {
                if (ltvLight.Items[i].ForeColor != System.Drawing.Color.Gray)
                {
                    isAllLightTurnedOff = false;
                    break;
                }
            }

            return isAllLightTurnedOff;
        }

        /// <summary>
        /// Light Enbale 1
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnLightEnbale1_Click(object sender, EventArgs e)
        {
            if (this.lightEnbaleLine1)
            {
                this.lightEnbaleLine1 = false;
                this.btnLightEnbale1.Text = "棚灯无效（F6）";
                this.btnLightEnbale1.FlatStyle = FlatStyle.Popup;
            }
            else
            {
                this.lightEnbaleLine1 = true;
                this.btnLightEnbale1.Text = "棚灯有效（F6）";
                this.btnLightEnbale1.FlatStyle = FlatStyle.Standard;

                if (this.ltvLight1Line1.Items.Count == 0)
                {
                    if (!string.Equals("0", this.txtRemainPiece1Line1.Text.Trim()))
                    {
                        this.txtRemainPiece1Line1.Text = string.Empty;
                    }
                }

                if (this.ltvLight2Line1.Items.Count == 0)
                {
                    if (!string.Equals("0", this.txtRemainPiece2Line1.Text.Trim()))
                    {
                        this.txtRemainPiece2Line1.Text = string.Empty;
                    }
                }

                if (this.ltvLight3Line1.Items.Count == 0)
                {
                    if (!string.Equals("0", this.txtRemainPiece3Line1.Text.Trim()))
                    {
                        this.txtRemainPiece3Line1.Text = string.Empty;
                    }
                }

                this.OpenAllSanner(ConfigInfo.LineCodeA);
            }
        }

        private void btnLightEnbale2_Click(object sender, EventArgs e)
        {
            if (this.lightEnbaleLine2)
            {
                this.lightEnbaleLine2 = false;
                this.btnLightEnbale2.Text = "棚灯无效（F7）";
                this.btnLightEnbale2.FlatStyle = FlatStyle.Popup;
            }
            else
            {
                this.lightEnbaleLine2 = true;
                this.btnLightEnbale2.Text = "棚灯有效（F7）";
                this.btnLightEnbale2.FlatStyle = FlatStyle.Standard;

                if (this.ltvLight1Line2.Items.Count == 0)
                {
                    if (!string.Equals("0", this.txtRemainPiece1Line2.Text.Trim()))
                    {
                        this.txtRemainPiece1Line2.Text = string.Empty;
                    }
                }

                if (this.ltvLight2Line2.Items.Count == 0)
                {
                    if (!string.Equals("0", this.txtRemainPiece2Line2.Text.Trim()))
                    {
                        this.txtRemainPiece2Line2.Text = string.Empty;
                    }
                }

                if (this.ltvLight3Line2.Items.Count == 0)
                {
                    if (!string.Equals("0", this.txtRemainPiece3Line2.Text.Trim()))
                    {
                        this.txtRemainPiece3Line2.Text = string.Empty;
                    }
                }

                this.OpenAllSanner(ConfigInfo.LineCodeB);
            }
        }

        private void btnLightEnbale3_Click(object sender, EventArgs e)
        {
            if (this.lightEnbaleLine3)
            {
                this.lightEnbaleLine3 = false;
                this.btnLightEnbale3.Text = "棚灯无效（F8）";
                this.btnLightEnbale3.FlatStyle = FlatStyle.Popup;
            }
            else
            {
                this.lightEnbaleLine3 = true;
                this.btnLightEnbale3.Text = "棚灯有效（F8）";
                this.btnLightEnbale3.FlatStyle = FlatStyle.Standard;

                if (this.ltvLight1Line3.Items.Count == 0)
                {
                    if (!string.Equals("0", this.txtRemainPiece1Line3.Text.Trim()))
                    {
                        this.txtRemainPiece1Line3.Text = string.Empty;
                    }
                }

                if (this.ltvLight2Line3.Items.Count == 0)
                {
                    if (!string.Equals("0", this.txtRemainPiece2Line3.Text.Trim()))
                    {
                        this.txtRemainPiece2Line3.Text = string.Empty;
                    }
                }

                if (this.ltvLight3Line3.Items.Count == 0)
                {
                    if (!string.Equals("0", this.txtRemainPiece3Line3.Text.Trim()))
                    {
                        this.txtRemainPiece3Line3.Text = string.Empty;
                    }
                }

                this.OpenAllSanner(ConfigInfo.LineCodeC);
            }
        }

        /// <summary>
        /// Return Event
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnReturn_Click(object sender, EventArgs e)
        {
            if (this.axAiPlug1 != null) CloseAllInfoLights(axAiPlug1);
            if (this.axAiPlug2 != null) CloseAllInfoLights(axAiPlug2);
            if (this.axAiPlug3 != null) CloseAllInfoLights(axAiPlug3);

            this.CloseCurrentWindow();
        }

        private void CloseAllInfoLights(AxAiPlugControl.AxAiPlug axAiPlug)
        {
            axAiPlug.SendCommand("T80010");
            axAiPlug.SendCommand("T80020");
            axAiPlug.SendCommand("T80030");
            axAiPlug.SendCommand("T80040");
            axAiPlug.SendCommand("T80050");
        }

        /// <summary>
        /// Form Resize
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void DistributionOperationView_SizeChanged(object sender, EventArgs e)
        {
            ////this.tlpWhole.Height = this.Height - 140;
        }

        /// <summary>
        /// Form Closing
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void DistributionOperationView_FormClosing(object sender, FormClosingEventArgs e)
        {
            this.axAiPlug1.SendCommand("T80010");
            this.axAiPlug1.SendCommand("T80020");
            this.axAiPlug1.SendCommand("T80030");
            this.axAiPlug1.SendCommand("T80040");
            this.axAiPlug1.SendCommand("T80050");
            this.axAiPlug1.StopPolling();
            this.axAiPlug1.Dispose();

            this.axAiPlug2.SendCommand("T80010");
            this.axAiPlug2.SendCommand("T80020");
            this.axAiPlug2.SendCommand("T80030");
            this.axAiPlug2.SendCommand("T80040");
            this.axAiPlug2.SendCommand("T80050");
            this.axAiPlug2.StopPolling();
            this.axAiPlug2.Dispose();

            this.axAiPlug3.SendCommand("T80010");
            this.axAiPlug3.SendCommand("T80020");
            this.axAiPlug3.SendCommand("T80030");
            this.axAiPlug3.SendCommand("T80040");
            this.axAiPlug3.SendCommand("T80050");
            this.axAiPlug3.StopPolling();
            this.axAiPlug3.Dispose();
        }

        /// <summary>
        /// Test
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnTest_Click(object sender, EventArgs e)
        {
            if ((Button)sender == btnTest)
            {
                if (this.scannerEnbaleLine1)
                {
                    AxAiPlugControl.__AiPlug_DataArrivalEvent e1
                        = new AxAiPlugControl.__AiPlug_DataArrivalEvent(this.cmbSebangoTest.Text);

                    this.axAiPlug1_DataArrival(null, e1);

                    this.cmbSerialPortTest.Items.Clear();
                    foreach (string item in this.lightNosWorker1Line1)
                    {
                        this.cmbSerialPortTest.Items.Add("t" + item + "00");
                    }

                    foreach (string item in this.lightNosWorker2Line1)
                    {
                        this.cmbSerialPortTest.Items.Add("t" + item + "00");
                    }

                    foreach (string item in this.lightNosWorker3Line1)
                    {
                        this.cmbSerialPortTest.Items.Add("t" + item + "00");
                    }
                }

                if (this.scannerEnbaleLine2)
                {
                    AxAiPlugControl.__AiPlug_DataArrivalEvent e1
                        = new AxAiPlugControl.__AiPlug_DataArrivalEvent(this.cmbSebangoTest.Text);

                    this.axAiPlug2_DataArrival(null, e1);

                    this.cmbSerialPortTest.Items.Clear();
                    foreach (string item in this.lightNosWorker1Line2)
                    {
                        this.cmbSerialPortTest.Items.Add("t" + item + "00");
                    }

                    foreach (string item in this.lightNosWorker2Line2)
                    {
                        this.cmbSerialPortTest.Items.Add("t" + item + "00");
                    }

                    foreach (string item in this.lightNosWorker3Line2)
                    {
                        this.cmbSerialPortTest.Items.Add("t" + item + "00");
                    }
                }

                if (this.scannerEnbaleLine3)
                {
                    AxAiPlugControl.__AiPlug_DataArrivalEvent e1
                        = new AxAiPlugControl.__AiPlug_DataArrivalEvent(this.cmbSebangoTest.Text);

                    this.axAiPlug3_DataArrival(null, e1);

                    this.cmbSerialPortTest.Items.Clear();
                    foreach (string item in this.lightNosWorker1Line3)
                    {
                        this.cmbSerialPortTest.Items.Add("t" + item + "00");
                    }

                    foreach (string item in this.lightNosWorker2Line3)
                    {
                        this.cmbSerialPortTest.Items.Add("t" + item + "00");
                    }

                    foreach (string item in this.lightNosWorker3Line3)
                    {
                        this.cmbSerialPortTest.Items.Add("t" + item + "00");
                    }
                }
            }

            if ((Button)sender == btnTest2)
            {
                AxAiPlugControl.__AiPlug_DataArrivalEvent e2
                    = new AxAiPlugControl.__AiPlug_DataArrivalEvent(this.cmbSerialPortTest.Text);

                this.axAiPlug1_DataArrival(null, e2);

                AxAiPlugControl.__AiPlug_DataArrivalEvent e3
                    = new AxAiPlugControl.__AiPlug_DataArrivalEvent(this.cmbSerialPortTest.Text);

                this.axAiPlug2_DataArrival(null, e3);

                AxAiPlugControl.__AiPlug_DataArrivalEvent e4
                    = new AxAiPlugControl.__AiPlug_DataArrivalEvent(this.cmbSerialPortTest.Text);

                this.axAiPlug3_DataArrival(null, e4);
            }
        }

        /// <summary>
        /// Clear
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnClearTest_Click(object sender, EventArgs e)
        {
            this.txtRemainPiece1Line1.Text = string.Empty;
            this.txtRemainPiece2Line1.Text = string.Empty;
            this.txtRemainPiece3Line1.Text = "0";

            this.ltvLight1Line1.Items.Clear();
            this.ltvLight2Line1.Items.Clear();
            this.ltvLight3Line1.Items.Clear();

            string message = "";

            this.txtStatus1.Text = message;

            this.btnLightEnbale1.Enabled = true;
            this.OpenAllSanner(ConfigInfo.LineCodeA);
            this.OpenAllSanner(ConfigInfo.LineCodeB);
            this.OpenAllSanner(ConfigInfo.LineCodeC);
        }


        /// <summary>
        /// axAiPlug Data Arrival
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void axAiPlug1_DataArrival(object sender, AxAiPlugControl.__AiPlug_DataArrivalEvent e)
        {
            string data = e.data;
            //Scanner:
            //data=t910111251012341001I
            //t:fixed, 9101:port no. of the scan gun, 
            //1:normal(0:exception), 12:the length of the barcode, 
            //51012341001I:barcode

            //LightTurnOff:
            //data=t000100xxxx
            //t:fixed, 0001:light no, 
            //00:normal(else:exception)

            data = data.Replace("\n", string.Empty);
            data = data.Replace("\r", string.Empty);

            if (data.Length < 5)
            {
                return;
            }

            string port = this.GetScannerPort(data);

            if (string.Equals(this.scannerPortWorker1Line1, port)
                || string.Equals(this.scannerPortWorker2Line1, port)
                || string.Equals(this.scannerPortWorker3Line1, port))
            {
                if (!this.IsLineAAllLightsOff())
                {
                    return;
                }

                if (!this.DoValidation(data, this.txtStatus1))
                {
                    return;
                }

                this.CloseAllSanner(ConfigInfo.LineCodeA);

                int barcodeLength = this.GetBarcodeLength(data);
                string sn = this.GetBarcode(data, barcodeLength);

                if (InterfaceUpdateHandle1 != null)
                {
                    this.Invoke(InterfaceUpdateHandle1,
                                new string[] { sn, port });
                }

                if (this.lightEnbaleLine1)
                {
                    this.btnReturn1.Enabled = false;
                    this.btnLightEnbale1.Enabled = false;
                }
            }
            else
            {
                if (LightTurnOffHandle1 != null)
                {
                    this.Invoke(LightTurnOffHandle1,
                                new string[] { data, string.Empty });
                }
            }

            //check the comment of the method IsLineAAllLightsOff
            if (this.IsLineAAllLightsOff())
            {
                this.OpenAllSanner(ConfigInfo.LineCodeA);
            }

            //所有流水线的残留台数是0并且流水线对应的队列里面没有序列号了才可以点返回按钮
            if (this.IsAllNoRemainPieceLine1() && this.IsAllQueueEmptyLine1())
            {
                this.btnReturn1.Enabled = true;
                this.btnLightEnbale1.Enabled = true;
            }
        }


        /// <summary>
        /// axAiPlug Data Arrival
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void axAiPlug2_DataArrival(object sender, AxAiPlugControl.__AiPlug_DataArrivalEvent e)
        {
            string data = e.data;
            //Scanner:
            //data=t910111251012341001I
            //t:fixed, 9101:port no. of the scan gun, 
            //1:normal(0:exception), 12:the length of the barcode, 
            //51012341001I:barcode

            //LightTurnOff:
            //data=t000100xxxx
            //t:fixed, 0001:light no, 
            //00:normal(else:exception)

            data = data.Replace("\n", string.Empty);
            data = data.Replace("\r", string.Empty);

            if (data.Length < 5)
            {
                return;
            }

            string port = this.GetScannerPort(data);

            if (string.Equals(this.scannerPortWorker1Line2, port)
                || string.Equals(this.scannerPortWorker2Line2, port)
                || string.Equals(this.scannerPortWorker3Line2, port))
            {
                if (!this.IsLineBAllLightsOff())
                {
                    return;
                }

                if (!this.DoValidation(data, this.txtStatus2))
                {
                    return;
                }

                this.CloseAllSanner(ConfigInfo.LineCodeB);

                int barcodeLength = this.GetBarcodeLength(data);
                string sn = this.GetBarcode(data, barcodeLength);

                if (InterfaceUpdateHandle2 != null)
                {
                    this.Invoke(InterfaceUpdateHandle2,
                                new string[] { sn, port });
                }

                if (this.lightEnbaleLine2)
                {
                    this.btnReturn2.Enabled = false;
                    this.btnLightEnbale2.Enabled = false;
                }
            }
            else
            {
                if (LightTurnOffHandle2 != null)
                {
                    this.Invoke(LightTurnOffHandle2,
                                new string[] { data, string.Empty });
                }
            }

            //check the comment of the method IsLineAAllLightsOff            
            if (this.IsLineBAllLightsOff())
            {
                this.OpenAllSanner(ConfigInfo.LineCodeB);
            }

            //所有流水线的残留台数是0并且流水线对应的队列里面没有序列号了才可以点返回按钮
            if (this.IsAllNoRemainPieceLine2() && this.IsAllQueueEmptyLine2())
            {
                this.btnReturn2.Enabled = true;
                this.btnLightEnbale2.Enabled = true;
            }
        }


        /// <summary>
        /// axAiPlug Data Arrival
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void axAiPlug3_DataArrival(object sender, AxAiPlugControl.__AiPlug_DataArrivalEvent e)
        {
            string data = e.data;
            //Scanner:
            //data=t910111251012341001I
            //t:fixed, 9101:port no. of the scan gun, 
            //1:normal(0:exception), 12:the length of the barcode, 
            //51012341001I:barcode

            //LightTurnOff:
            //data=t000100xxxx
            //t:fixed, 0001:light no, 
            //00:normal(else:exception)

            data = data.Replace("\n", string.Empty);
            data = data.Replace("\r", string.Empty);

            if (data.Length < 5)
            {
                return;
            }

            string port = this.GetScannerPort(data);

            if (string.Equals(this.scannerPortWorker1Line3, port)
                || string.Equals(this.scannerPortWorker2Line3, port)
                || string.Equals(this.scannerPortWorker3Line3, port))
            {
                if (!this.IsLineCAllLightsOff())
                {
                    return;
                }

                if (!this.DoValidation(data, this.txtStatus3))
                {
                    return;
                }

                this.CloseAllSanner(ConfigInfo.LineCodeC);

                int barcodeLength = this.GetBarcodeLength(data);
                string sn = this.GetBarcode(data, barcodeLength);

                if (InterfaceUpdateHandle3 != null)
                {
                    this.Invoke(InterfaceUpdateHandle3,
                                new string[] { sn, port });
                }

                if (this.lightEnbaleLine3)
                {
                    this.btnReturn3.Enabled = false;
                    this.btnLightEnbale3.Enabled = false;
                }
            }
            else
            {
                if (LightTurnOffHandle3 != null)
                {
                    this.Invoke(LightTurnOffHandle3,
                                new string[] { data, string.Empty });
                }
            }

            //check the comment of the method IsLineAAllLightsOff            
            if (this.IsLineCAllLightsOff())
            {
                this.OpenAllSanner(ConfigInfo.LineCodeC);
            }

            //所有流水线的残留台数是0并且流水线对应的队列里面没有序列号了才可以点返回按钮
            if (this.IsAllNoRemainPieceLine3() && this.IsAllQueueEmptyLine3())
            {
                this.btnReturn3.Enabled = true;
                this.btnLightEnbale3.Enabled = true;
            }
        }


        private bool IsAllQueueEmptyLine1()
        {
            bool isAllQueueEmpty = true;
            for (int i = 0; i < this.currentWorkerCountLine1; i++)
            {
                if (QueueListLine1[i].Count > 0) isAllQueueEmpty = false;
            }
            return isAllQueueEmpty;
        }

        private bool IsAllQueueEmptyLine2()
        {
            bool isAllQueueEmpty = true;
            for (int i = 0; i < this.currentWorkerCountLine2; i++)
            {
                if (QueueListLine2[i].Count > 0) isAllQueueEmpty = false;
            }
            return isAllQueueEmpty;
        }

        private bool IsAllQueueEmptyLine3()
        {
            bool isAllQueueEmpty = true;
            for (int i = 0; i < this.currentWorkerCountLine3; i++)
            {
                if (QueueListLine3[i].Count > 0) isAllQueueEmpty = false;
            }
            return isAllQueueEmpty;
        }


        //如果流水线A的残留台数是0（只有当A的所有棚灯全部熄灭后A的残留台数才会是0）
        //并且打开的最后一条流水线的剩余条码数（保存在最后一条流水线对应的队列中）与流水线A的剩余条码数的差额小于配置文件中
        //GapToStopScan的值时，扫描枪才可以继续打开使用
        private bool IsLineAAllLightsOff()
        {
            bool isLineALightsClear = false;
            if (string.IsNullOrEmpty(this.txtRemainPiece1Line1.Text.Trim()) || string.Equals(this.txtRemainPiece1Line1.Text.Trim(), "0"))
            {
                isLineALightsClear = true;
            }

            string gapToStopScan = ConfigurationManager.AppSettings["GapToStopScan"];
            int gap;
            if (!int.TryParse(gapToStopScan, out gap)) gap = 10;

            Queue<string> lastQueue = QueueListLine1[this.currentWorkerCountLine1 - 1];
            Queue<string> QueueA = QueueListLine1[0];
            if (lastQueue.Count - QueueA.Count < gap) return (true & isLineALightsClear);
            else return false;
        }


        private bool IsLineBAllLightsOff()
        {
            bool isLineBLightsClear = false;
            if (string.IsNullOrEmpty(this.txtRemainPiece1Line2.Text.Trim()) || string.Equals(this.txtRemainPiece1Line2.Text.Trim(), "0"))
            {
                isLineBLightsClear = true;
            }

            string gapToStopScan = ConfigurationManager.AppSettings["GapToStopScan"];
            int gap;
            if (!int.TryParse(gapToStopScan, out gap)) gap = 10;

            Queue<string> lastQueue = QueueListLine1[this.currentWorkerCountLine2 - 1];
            Queue<string> QueueA = QueueListLine1[0];
            if (lastQueue.Count - QueueA.Count < gap) return (true & isLineBLightsClear);
            else return false;
        }


        private bool IsLineCAllLightsOff()
        {
            bool isLineCLightsClear = false;
            if (string.IsNullOrEmpty(this.txtRemainPiece1Line3.Text.Trim()) || string.Equals(this.txtRemainPiece1Line3.Text.Trim(), "0"))
            {
                isLineCLightsClear = true;
            }

            string gapToStopScan = ConfigurationManager.AppSettings["GapToStopScan"];
            int gap;
            if (!int.TryParse(gapToStopScan, out gap)) gap = 10;

            Queue<string> lastQueue = QueueListLine1[this.currentWorkerCountLine3 - 1];
            Queue<string> QueueA = QueueListLine1[0];
            if (lastQueue.Count - QueueA.Count < gap) return (true & isLineCLightsClear);
            else return false;
        }

    }
}
